Skip to content

Commit 4491f0b

Browse files
committed
[IR] Remove unnecessary bitcast from CreateMalloc()
This bitcast is no longer necessary with opaque pointers. This results in some annoying variable name changes in tests.
1 parent 162bafc commit 4491f0b

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

llvm/lib/IR/Instructions.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -868,24 +868,12 @@ static Instruction *createMalloc(Instruction *InsertBefore,
868868
if (!MallocFunc)
869869
// prototype malloc as "void *malloc(size_t)"
870870
MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
871-
PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
872871
CallInst *MCall = nullptr;
873-
Instruction *Result = nullptr;
874872
if (InsertBefore) {
875-
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
873+
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, Name,
876874
InsertBefore);
877-
Result = MCall;
878-
if (Result->getType() != AllocPtrType)
879-
// Create a cast instruction to convert to the right type...
880-
Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
881875
} else {
882-
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
883-
Result = MCall;
884-
if (Result->getType() != AllocPtrType) {
885-
MCall->insertInto(InsertAtEnd, InsertAtEnd->end());
886-
// Create a cast instruction to convert to the right type...
887-
Result = new BitCastInst(MCall, AllocPtrType, Name);
888-
}
876+
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, Name);
889877
}
890878
MCall->setTailCall();
891879
if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) {
@@ -895,7 +883,7 @@ static Instruction *createMalloc(Instruction *InsertBefore,
895883
}
896884
assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
897885

898-
return Result;
886+
return MCall;
899887
}
900888

901889
/// CreateMalloc - Generate the IR for a call to malloc:

llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ entry:
110110
call void @foo()
111111
ret void
112112
; CHECK: call.em.longjmp:
113-
; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %__THREW__.val, %if.then1 ], [ %__THREW__.val3, %if.then14 ]
114-
; CHECK-NEXT: %threwvalue.phi = phi i32 [ %__threwValue.val, %if.then1 ], [ %__threwValue.val7, %if.then14 ]
113+
; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %__THREW__.val, %if.then1 ], [ %__THREW__.val4, %if.then15 ]
114+
; CHECK-NEXT: %threwvalue.phi = phi i32 [ %__threwValue.val, %if.then1 ], [ %__threwValue.val8, %if.then15 ]
115115
; CHECK-NEXT: tail call void @free(ptr %[[SETJMP_TABLE1]])
116116
; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %threw.phi, i32 %threwvalue.phi)
117117
; CHECK-NEXT: unreachable
@@ -158,9 +158,9 @@ if.then: ; preds = %entry
158158
; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
159159

160160
; CHECK: if.then.split:
161-
; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR2:.*]], %if.end2 ], [ %[[VAR0]], %if.then ]
162-
; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end2 ]
163-
; CHECK: %[[SETJMP_TABLE2:.*]] = phi ptr [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end2 ]
161+
; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR2:.*]], %if.end3 ], [ %[[VAR0]], %if.then ]
162+
; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
163+
; CHECK: %[[SETJMP_TABLE2:.*]] = phi ptr [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
164164
; CHECK: store i32 %[[VAR1]], ptr @global_var, align 4
165165

166166
if.end: ; preds = %if.then, %entry
@@ -169,7 +169,7 @@ if.end: ; preds = %if.then, %entry
169169
; CHECK: if.end:
170170
; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry.split ]
171171
; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry.split ]
172-
; CHECK: %[[SETJMP_TABLE3]] = phi ptr [ %[[SETJMP_TABLE2]], %if.then.split ], [ %malloccall, %entry.split ]
172+
; CHECK: %[[SETJMP_TABLE3]] = phi ptr [ %[[SETJMP_TABLE2]], %if.then.split ], [ %setjmpTable, %entry.split ]
173173
}
174174

175175
; Test a case when a function only calls other functions that are neither setjmp nor longjmp

llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ catch: ; preds = %catch.start
108108
call void @__cxa_end_catch() [ "funclet"(token %2) ]
109109
catchret from %2 to label %catchret.dest
110110
; CHECK: catch: ; preds = %catch.start
111-
; CHECK-NEXT: %exn = load ptr, ptr %exn.slot14, align 4
111+
; CHECK-NEXT: %exn = load ptr, ptr %exn.slot15, align 4
112112
; CHECK-NEXT: %5 = call ptr @__cxa_begin_catch(ptr %exn) #7 [ "funclet"(token %2) ]
113113
; CHECK-NEXT: invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
114114
; CHECK-NEXT: to label %.noexc unwind label %catch.dispatch.longjmp
@@ -230,9 +230,9 @@ terminate: ; preds = %ehcleanup
230230
; change to an invoke whose unwind destination is determined by its parent
231231
; chain.
232232
; CHECK-NEXT: invoke void @terminate() {{.*}} [ "funclet"(token [[T2]]) ]
233-
; CHECK-NEXT: to label %.noexc3 unwind label %catch.dispatch5
233+
; CHECK-NEXT: to label %[[NOEXC:.*]] unwind label %catch.dispatch5
234234

235-
; CHECK: .noexc3:
235+
; CHECK: [[NOEXC]]:
236236
; CHECK-NEXT: unreachable
237237

238238
; CHECK: catch.dispatch.longjmp:
@@ -265,7 +265,7 @@ ehcleanup: ; preds = %entry
265265
; (cleanuppad), whose parent is 'none', so we should unwind directly to
266266
; %catch.dispatch.longjmp.
267267
%call2 = call noundef ptr @_ZN4TempD2Ev(ptr noundef %t) #2 [ "funclet"(token %0) ]
268-
; CHECK: %call23 = invoke {{.*}} ptr @_ZN4TempD2Ev(ptr
268+
; CHECK: %call13 = invoke {{.*}} ptr @_ZN4TempD2Ev(ptr
269269
; CHECK-NEXT: to label {{.*}} unwind label %catch.dispatch.longjmp
270270
cleanupret from %0 unwind to caller
271271
}

llvm/test/CodeGen/WebAssembly/lower-wasm-sjlj.ll

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ entry:
2525
unreachable
2626

2727
; CHECK: entry:
28-
; CHECK-NEXT: %malloccall = tail call ptr @malloc([[PTR]] 40)
29-
; CHECK-NEXT: store i32 0, ptr %malloccall, align 4
28+
; CHECK-NEXT: %setjmpTable = tail call ptr @malloc([[PTR]] 40)
29+
; CHECK-NEXT: store i32 0, ptr %setjmpTable, align 4
3030
; CHECK-NEXT: %setjmpTableSize = add i32 4, 0
3131
; CHECK-NEXT: br label %setjmp.dispatch
3232

3333
; CHECK: setjmp.dispatch:
34-
; CHECK-NEXT: %val8 = phi i32 [ %val, %if.end ], [ undef, %entry ]
35-
; CHECK-NEXT: %buf7 = phi ptr [ %buf6, %if.end ], [ undef, %entry ]
36-
; CHECK-NEXT: %setjmpTableSize4 = phi i32 [ %setjmpTableSize5, %if.end ], [ %setjmpTableSize, %entry ]
37-
; CHECK-NEXT: %setjmpTable2 = phi ptr [ %setjmpTable3, %if.end ], [ %malloccall, %entry ]
34+
; CHECK-NEXT: %[[VAL2:.*]] = phi i32 [ %val, %if.end ], [ undef, %entry ]
35+
; CHECK-NEXT: %[[BUF:.*]] = phi ptr [ %[[BUF2:.*]], %if.end ], [ undef, %entry ]
36+
; CHECK-NEXT: %[[SETJMPTABLESIZE2:.*]] = phi i32 [ %[[SETJMPTABLESIZE3:.*]], %if.end ], [ %setjmpTableSize, %entry ]
37+
; CHECK-NEXT: %[[SETJMPTABLE2:.*]] = phi ptr [ %[[SETJMPTABLE3:.*]], %if.end ], [ %setjmpTable, %entry ]
3838
; CHECK-NEXT: %label.phi = phi i32 [ %label, %if.end ], [ -1, %entry ]
3939
; CHECK-NEXT: switch i32 %label.phi, label %entry.split [
4040
; CHECK-NEXT: i32 1, label %entry.split.split
4141
; CHECK-NEXT: ]
4242

4343
; CHECK: entry.split:
4444
; CHECK-NEXT: %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
45-
; CHECK-NEXT: %setjmpTable = call ptr @saveSetjmp(ptr %buf, i32 1, ptr %setjmpTable2, i32 %setjmpTableSize4)
46-
; CHECK-NEXT: %setjmpTableSize1 = call i32 @getTempRet0()
45+
; CHECK-NEXT: %[[SETJMPTABLE4:.*]] = call ptr @saveSetjmp(ptr %buf, i32 1, ptr %[[SETJMPTABLE2]], i32 %[[SETJMPTABLESIZE2]])
46+
; CHECK-NEXT: %[[SETJMPTABLESIZE4:.*]] = call i32 @getTempRet0()
4747
; CHECK-NEXT: br label %entry.split.split
4848

4949
; CHECK: entry.split.split:
50-
; CHECK-NEXT: %buf6 = phi ptr [ %buf7, %setjmp.dispatch ], [ %buf, %entry.split ]
51-
; CHECK-NEXT: %setjmpTableSize5 = phi i32 [ %setjmpTableSize1, %entry.split ], [ %setjmpTableSize4, %setjmp.dispatch ]
52-
; CHECK-NEXT: %setjmpTable3 = phi ptr [ %setjmpTable, %entry.split ], [ %setjmpTable2, %setjmp.dispatch ]
53-
; CHECK-NEXT: %setjmp.ret = phi i32 [ 0, %entry.split ], [ %val8, %setjmp.dispatch ]
54-
; CHECK-NEXT: invoke void @__wasm_longjmp(ptr %buf6, i32 1)
50+
; CHECK-NEXT: %[[BUF2]] = phi ptr [ %[[BUF]], %setjmp.dispatch ], [ %buf, %entry.split ]
51+
; CHECK-NEXT: %[[SETJMPTABLESIZE3]] = phi i32 [ %[[SETJMPTABLESIZE4]], %entry.split ], [ %[[SETJMPTABLESIZE2]], %setjmp.dispatch ]
52+
; CHECK-NEXT: %[[SETJMPTABLE3]] = phi ptr [ %[[SETJMPTABLE4]], %entry.split ], [ %[[SETJMPTABLE2]], %setjmp.dispatch ]
53+
; CHECK-NEXT: %setjmp.ret = phi i32 [ 0, %entry.split ], [ %[[VAL2]], %setjmp.dispatch ]
54+
; CHECK-NEXT: invoke void @__wasm_longjmp(ptr %[[BUF2]], i32 1)
5555
; CHECK-NEXT: to label %.noexc unwind label %catch.dispatch.longjmp
5656

5757
; CHECK: .noexc:
@@ -68,12 +68,12 @@ entry:
6868
; CHECK-NEXT: %env = load ptr, ptr %env_gep, align {{.*}}
6969
; CHECK-NEXT: %val = load i32, ptr %val_gep, align 4
7070
; CHECK-NEXT: %setjmp.id = load [[PTR]], ptr %env, align {{.*}}
71-
; CHECK-NEXT: %label = call i32 @testSetjmp([[PTR]] %setjmp.id, ptr %setjmpTable3, i32 %setjmpTableSize5) [ "funclet"(token %1) ]
71+
; CHECK-NEXT: %label = call i32 @testSetjmp([[PTR]] %setjmp.id, ptr %[[SETJMPTABLE3]], i32 %[[SETJMPTABLESIZE3]]) [ "funclet"(token %1) ]
7272
; CHECK-NEXT: %2 = icmp eq i32 %label, 0
7373
; CHECK-NEXT: br i1 %2, label %if.then, label %if.end
7474

7575
; CHECK: if.then:
76-
; CHECK-NEXT: tail call void @free(ptr %setjmpTable3) [ "funclet"(token %1) ]
76+
; CHECK-NEXT: tail call void @free(ptr %[[SETJMPTABLE3]]) [ "funclet"(token %1) ]
7777
; CHECK-NEXT: call void @__wasm_longjmp(ptr %env, i32 %val) [ "funclet"(token %1) ]
7878
; CHECK-NEXT: unreachable
7979

polly/test/CodeGen/MemAccess/create_arrays_heap.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@
3131
; %E : size(E) = 270336*200000*8 = 432537600000 cast to double*
3232
; %F : size(F) = 270336*8 = 2162688 cast to i64*
3333
; CODEGEN: polly.start:
34-
; CODEGEN: %malloccall = tail call ptr @malloc(i64 2162688)
35-
; CODEGEN: %malloccall1 = tail call ptr @malloc(i64 432537600000)
36-
; CODEGEN: %malloccall2 = tail call ptr @malloc(i64 2162688)
34+
; CODEGEN: %D = tail call ptr @malloc(i64 2162688)
35+
; CODEGEN: %E = tail call ptr @malloc(i64 432537600000)
36+
; CODEGEN: %F = tail call ptr @malloc(i64 2162688)
3737
;
3838
; Check if there are the 3 expected malloc calls with the right parameters at polly.exiting.
3939
; Cast to i8* before freeing because malloc give us a i8 and free is waiting for a i8*
4040
; CODEGEN: polly.exiting:
41-
; CODEGEN: tail call void @free(ptr %malloccall)
42-
; CODEGEN: tail call void @free(ptr %malloccall1)
43-
; CODEGEN: tail call void @free(ptr %malloccall2)
41+
; CODEGEN: tail call void @free(ptr %D)
42+
; CODEGEN: tail call void @free(ptr %E)
43+
; CODEGEN: tail call void @free(ptr %F)
4444
;
4545
; Check if the new access for array E is present.
4646
; CODEGEN: %polly.access.mul.{{.*}} = mul nsw i64 %polly.indvar, 200000
4747
; CODEGEN: %polly.access.add.{{.*}} = add nsw i64 %polly.access.mul.{{.*}}, %
48-
; CODEGEN: %polly.access.{{.*}} = getelementptr double, ptr %malloccall1, i64 %polly.access.add.{{.*}}
48+
; CODEGEN: %polly.access.{{.*}} = getelementptr double, ptr %E, i64 %polly.access.add.{{.*}}
4949
;
5050
; ModuleID = 'create_arrays_heap.ll'
5151
;

0 commit comments

Comments
 (0)