llvm.org GIT mirror llvm / 9adc0ab
Move EVER MORE stuff over to LLVMContext. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75703 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
53 changed file(s) with 427 addition(s) and 440 deletion(s). Raw diff Collapse all Expand all
4242 comflag = cf;
4343
4444 header(Context);
45 readloop(0, 0, 0);
45 readloop(0, 0, 0, Context);
4646 delete builder;
4747 return module;
4848 }
7676 builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func));
7777
7878 //%arr = malloc i8, i32 %d
79 ConstantInt *val_mem = ConstantInt::get(APInt(32, memtotal));
79 ConstantInt *val_mem = C.getConstantInt(APInt(32, memtotal));
8080 ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
8181
8282 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
8383 {
8484 Value *memset_params[] = {
8585 ptr_arr,
86 ConstantInt::get(APInt(8, 0)),
86 C.getConstantInt(APInt(8, 0)),
8787 val_mem,
88 ConstantInt::get(APInt(32, 1))
88 C.getConstantInt(APInt(32, 1))
8989 };
9090
9191 CallInst *memset_call = builder->
9696 //%arrmax = getelementptr i8 *%arr, i32 %d
9797 if (comflag & flag_arraybounds) {
9898 ptr_arrmax = builder->
99 CreateGEP(ptr_arr, ConstantInt::get(APInt(32, memtotal)), "arrmax");
99 CreateGEP(ptr_arr, C.getConstantInt(APInt(32, memtotal)), "arrmax");
100100 }
101101
102102 //%head.%d = getelementptr i8 *%arr, i32 %d
103103 curhead = builder->CreateGEP(ptr_arr,
104 ConstantInt::get(APInt(32, memtotal/2)),
104 C.getConstantInt(APInt(32, memtotal/2)),
105105 headreg);
106106
107107
123123 if (comflag & flag_arraybounds)
124124 {
125125 //@aberrormsg = internal constant [%d x i8] c"\00"
126 Constant *msg_0 = ConstantArray::
127 get("Error: The head has left the tape.", true);
126 Constant *msg_0 =
127 C.getConstantArray("Error: The head has left the tape.", true);
128128
129129 GlobalVariable *aberrormsg = new GlobalVariable(
130130 *module,
137137 //declare i32 @puts(i8 *)
138138 Function *puts_func = cast(module->
139139 getOrInsertFunction("puts", IntegerType::Int32Ty,
140 PointerType::getUnqual(IntegerType::Int8Ty), NULL));
140 C.getPointerTypeUnqual(IntegerType::Int8Ty), NULL));
141141
142142 //brainf.aberror:
143143 aberrorbb = BasicBlock::Create(label, brainf_func);
171171 }
172172 }
173173
174 void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb) {
174 void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
175 LLVMContext &C) {
175176 Symbol cursym = SYM_NONE;
176177 int curvalue = 0;
177178 Symbol nextsym = SYM_NONE;
227228 {
228229 //%head.%d = getelementptr i8 *%head.%d, i32 %d
229230 curhead = builder->
230 CreateGEP(curhead, ConstantInt::get(APInt(32, curvalue)),
231 CreateGEP(curhead, C.getConstantInt(APInt(32, curvalue)),
231232 headreg);
232233
233234 //Error block for array out of bounds
262263
263264 //%tape.%d = add i8 %tape.%d, %d
264265 Value *tape_1 = builder->
265 CreateAdd(tape_0, ConstantInt::get(APInt(8, curvalue)), tapereg);
266 CreateAdd(tape_0, C.getConstantInt(APInt(8, curvalue)), tapereg);
266267
267268 //store i8 %tape.%d, i8 *%head.%d\n"
268269 builder->CreateStore(tape_1, curhead);
282283
283284 // Make part of PHI instruction now, wait until end of loop to finish
284285 PHINode *phi_0 =
285 PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty),
286 PHINode::Create(C.getPointerTypeUnqual(IntegerType::Int8Ty),
286287 headreg, testbb);
287288 phi_0->reserveOperandSpace(2);
288289 phi_0->addIncoming(curhead, bb_0);
289290 curhead = phi_0;
290291
291 readloop(phi_0, bb_1, testbb);
292 readloop(phi_0, bb_1, testbb, C);
292293 }
293294 break;
294295
427428
428429 //%test.%d = icmp eq i8 %tape.%d, 0
429430 ICmpInst *test_0 = new ICmpInst(*testbb, ICmpInst::ICMP_EQ, tape_0,
430 ConstantInt::get(APInt(8, 0)), testreg);
431 C.getConstantInt(APInt(8, 0)), testreg);
431432
432433 //br i1 %test.%d, label %main.%d, label %main.%d
433434 BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
438439
439440 //%head.%d = phi i8 *[%head.%d, %main.%d]
440441 PHINode *phi_1 = builder->
441 CreatePHI(PointerType::getUnqual(IntegerType::Int8Ty), headreg);
442 CreatePHI(C.getPointerTypeUnqual(IntegerType::Int8Ty), headreg);
442443 phi_1->reserveOperandSpace(1);
443444 phi_1->addIncoming(head_0, testbb);
444445 curhead = phi_1;
6969
7070 /// The main loop for parsing. It calls itself recursively
7171 /// to handle the depth of nesting of "[]".
72 void readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb);
72 void readloop(PHINode *phi, BasicBlock *oldbb,
73 BasicBlock *testbb, LLVMContext &Context);
7374
7475 /// Constants during parsing
7576 int memtotal;
3535 #include "llvm/Support/raw_ostream.h"
3636 using namespace llvm;
3737
38 static Function *CreateFibFunction(Module *M) {
38 static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
3939 // Create the fib function and insert it into module M. This function is said
4040 // to return an int and take an int parameter.
4141 Function *FibF =
4646 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
4747
4848 // Get pointers to the constants.
49 Value *One = ConstantInt::get(Type::Int32Ty, 1);
50 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
49 Value *One = Context.getConstantInt(Type::Int32Ty, 1);
50 Value *Two = Context.getConstantInt(Type::Int32Ty, 2);
5151
5252 // Get pointer to the integer argument of the add1 function...
5353 Argument *ArgX = FibF->arg_begin(); // Get the arg.
9696 Module *M = new Module("test", Context);
9797
9898 // We are about to create the "fib" function:
99 Function *FibF = CreateFibFunction(M);
99 Function *FibF = CreateFibFunction(M, Context);
100100
101101 // Now we going to create JIT
102102 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
6868 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
6969
7070 // Get pointers to the constant `1'.
71 Value *One = ConstantInt::get(Type::Int32Ty, 1);
71 Value *One = Context.getConstantInt(Type::Int32Ty, 1);
7272
7373 // Get pointers to the integer argument of the add1 function...
7474 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
9393 BB = BasicBlock::Create("EntryBlock", FooF);
9494
9595 // Get pointers to the constant `10'.
96 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
96 Value *Ten = Context.getConstantInt(Type::Int32Ty, 10);
9797
9898 // Pass Ten to the call call:
9999 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
2929 Module *M = new Module("test", Context);
3030
3131 // Create the main function: first create the type 'int ()'
32 FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
32 FunctionType *FT =
33 Context.getFunctionType(Type::Int32Ty, /*not vararg*/false);
3334
3435 // By passing a module as the last parameter to the Function constructor,
3536 // it automatically gets appended to the Module.
4041 BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
4142
4243 // Get pointers to the constant integers...
43 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
44 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
44 Value *Two = Context.getConstantInt(Type::Int32Ty, 2);
45 Value *Three = Context.getConstantInt(Type::Int32Ty, 3);
4546
4647 // Create the add instruction... does not insert...
4748 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
4343 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
4444
4545 // Get pointers to the constant `1'.
46 Value *One = ConstantInt::get(Type::Int32Ty, 1);
46 Value *One = M->getContext().getConstantInt(Type::Int32Ty, 1);
4747
4848 // Get pointers to the integer argument of the add1 function...
4949 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
7171 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
7272
7373 // Get pointers to the constants.
74 Value *One = ConstantInt::get(Type::Int32Ty, 1);
75 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
74 Value *One = M->getContext().getConstantInt(Type::Int32Ty, 1);
75 Value *Two = M->getContext().getConstantInt(Type::Int32Ty, 2);
7676
7777 // Get pointer to the integer argument of the add1 function...
7878 Argument *ArgX = FibF->arg_begin(); // Get the arg.
101101 return CreateTrueFalseVals(false);
102102 }
103103
104 /// Return a ConstantInt with the specified integer value for the specified
105 /// type. If the type is wider than 64 bits, the value will be zero-extended
106 /// to fit the type, unless isSigned is true, in which case the value will
107 /// be interpreted as a 64-bit signed integer and sign-extended to fit
108 /// the type.
109 /// @brief Get a ConstantInt for a specific value.
110 static ConstantInt *get(const IntegerType *Ty,
111 uint64_t V, bool isSigned = false);
112
113 /// If Ty is a vector type, return a Constant with a splat of the given
114 /// value. Otherwise return a ConstantInt for the given value.
115 static Constant *get(const Type *Ty, uint64_t V, bool isSigned = false);
116
117 /// Return a ConstantInt with the specified value for the specified type. The
118 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
119 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
120 /// signed value for the type Ty.
121 /// @brief Get a ConstantInt for a specific signed value.
122 static ConstantInt *getSigned(const IntegerType *Ty, int64_t V) {
123 return get(Ty, V, true);
124 }
125 static Constant *getSigned(const Type *Ty, int64_t V) {
126 return get(Ty, V, true);
127 }
128
129104 /// Return a ConstantInt with the specified value and an implied Type. The
130105 /// type is the integer type that corresponds to the bit width of the value.
131106 static ConstantInt *get(const APInt &V);
132
133 /// If Ty is a vector type, return a Constant with a splat of the given
134 /// value. Otherwise return a ConstantInt for the given value.
135 static Constant *get(const Type *Ty, const APInt &V);
136107
137108 /// getType - Specialize the getType() method to always return an IntegerType,
138109 /// which reduces the amount of casting needed in parts of the compiler.
346317 // FIXME: make this the primary ctor method.
347318 return get(T, std::vector(Vals, Vals+NumVals));
348319 }
349
350 /// This method constructs a ConstantArray and initializes it with a text
351 /// string. The default behavior (AddNull==true) causes a null terminator to
352 /// be placed at the end of the array. This effectively increases the length
353 /// of the array by one (you've been warned). However, in some situations
354 /// this is not desired so if AddNull==false then the string is copied without
355 /// null termination.
356 static Constant *get(const std::string &Initializer, bool AddNull = true);
357320
358321 /// Transparently provide more efficient getOperand methods.
359322 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
2828 class ConstantInt;
2929 class ConstantRange;
3030 class APInt;
31 class LLVMContext;
3132
3233 //===----------------------------------------------------------------------===//
3334 // AllocationInst Class
3839 ///
3940 class AllocationInst : public UnaryInstruction {
4041 protected:
41 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
42 const std::string &Name = "", Instruction *InsertBefore = 0);
43 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
44 const std::string &Name, BasicBlock *InsertAtEnd);
42 LLVMContext &Context;
43
44 AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
45 unsigned iTy, unsigned Align, const std::string &Name = "",
46 Instruction *InsertBefore = 0);
47 AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
48 unsigned iTy, unsigned Align, const std::string &Name,
49 BasicBlock *InsertAtEnd);
4550 public:
4651 // Out of line virtual method, so the vtable, etc. has a home.
4752 virtual ~AllocationInst();
97102 class MallocInst : public AllocationInst {
98103 MallocInst(const MallocInst &MI);
99104 public:
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 explicit MallocInst(LLVMContext &Context,
106 const Type *Ty, Value *ArraySize = 0,
101107 const std::string &NameStr = "",
102108 Instruction *InsertBefore = 0)
103 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
109 : AllocationInst(Context, Ty, ArraySize, Malloc,
110 0, NameStr, InsertBefore) {}
111 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
112 const std::string &NameStr, BasicBlock *InsertAtEnd)
113 : AllocationInst(Context, Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
114
115 MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
116 Instruction *InsertBefore = 0)
117 : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
118 MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
105119 BasicBlock *InsertAtEnd)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
107
108 MallocInst(const Type *Ty, const std::string &NameStr,
120 : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
121
122 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
123 unsigned Align, const std::string &NameStr,
124 BasicBlock *InsertAtEnd)
125 : AllocationInst(Context, Ty, ArraySize, Malloc,
126 Align, NameStr, InsertAtEnd) {}
127 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
128 unsigned Align, const std::string &NameStr = "",
109129 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &NameStr,
112 BasicBlock *InsertAtEnd)
113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
114
115 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
116 const std::string &NameStr, BasicBlock *InsertAtEnd)
117 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
118 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
119 const std::string &NameStr = "",
120 Instruction *InsertBefore = 0)
121 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
130 : AllocationInst(Context, Ty, ArraySize,
131 Malloc, Align, NameStr, InsertBefore) {}
122132
123133 virtual MallocInst *clone(LLVMContext &Context) const;
124134
142152 class AllocaInst : public AllocationInst {
143153 AllocaInst(const AllocaInst &);
144154 public:
145 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
155 explicit AllocaInst(LLVMContext &Context, const Type *Ty,
156 Value *ArraySize = 0,
146157 const std::string &NameStr = "",
147158 Instruction *InsertBefore = 0)
148 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
149 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
159 : AllocationInst(Context, Ty, ArraySize, Alloca,
160 0, NameStr, InsertBefore) {}
161 AllocaInst(LLVMContext &Context, const Type *Ty,
162 Value *ArraySize, const std::string &NameStr,
150163 BasicBlock *InsertAtEnd)
151 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
152
153 AllocaInst(const Type *Ty, const std::string &NameStr,
164 : AllocationInst(Context, Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
165
166 AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
154167 Instruction *InsertBefore = 0)
155 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
156 AllocaInst(const Type *Ty, const std::string &NameStr,
168 : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
169 AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
157170 BasicBlock *InsertAtEnd)
158 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
159
160 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
161 const std::string &NameStr = "", Instruction *InsertBefore = 0)
162 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
163 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
164 const std::string &NameStr, BasicBlock *InsertAtEnd)
165 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
171 : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
172
173 AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
174 unsigned Align, const std::string &NameStr = "",
175 Instruction *InsertBefore = 0)
176 : AllocationInst(Context, Ty, ArraySize, Alloca,
177 Align, NameStr, InsertBefore) {}
178 AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
179 unsigned Align, const std::string &NameStr,
180 BasicBlock *InsertAtEnd)
181 : AllocationInst(Context, Ty, ArraySize, Alloca,
182 Align, NameStr, InsertAtEnd) {}
166183
167184 virtual AllocaInst *clone(LLVMContext &Context) const;
168185
12651282 }
12661283 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
12671284 Instruction *InsertBefore = 0);
1268 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1269 Instruction *InsertBefore = 0);
12701285 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1271 BasicBlock *InsertAtEnd);
1272 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
12731286 BasicBlock *InsertAtEnd);
12741287
12751288 /// isValidOperands - Return true if an extractelement instruction can be
13091322 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
13101323 const std::string &NameStr = "",
13111324 Instruction *InsertBefore = 0);
1312 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1313 const std::string &NameStr = "",
1314 Instruction *InsertBefore = 0);
13151325 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1316 const std::string &NameStr, BasicBlock *InsertAtEnd);
1317 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
13181326 const std::string &NameStr, BasicBlock *InsertAtEnd);
13191327 public:
13201328 static InsertElementInst *Create(const InsertElementInst &IE) {
13251333 Instruction *InsertBefore = 0) {
13261334 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
13271335 }
1328 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1329 const std::string &NameStr = "",
1330 Instruction *InsertBefore = 0) {
1331 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1332 }
13331336 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1334 const std::string &NameStr,
1335 BasicBlock *InsertAtEnd) {
1336 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1337 }
1338 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
13391337 const std::string &NameStr,
13401338 BasicBlock *InsertAtEnd) {
13411339 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
6969 // ConstantInt accessors
7070 ConstantInt* getConstantIntTrue();
7171 ConstantInt* getConstantIntFalse();
72
73 /// If Ty is a vector type, return a Constant with a splat of the given
74 /// value. Otherwise return a ConstantInt for the given value.
7275 Constant* getConstantInt(const Type* Ty, uint64_t V,
7376 bool isSigned = false);
77
78 /// Return a ConstantInt with the specified integer value for the specified
79 /// type. If the type is wider than 64 bits, the value will be zero-extended
80 /// to fit the type, unless isSigned is true, in which case the value will
81 /// be interpreted as a 64-bit signed integer and sign-extended to fit
82 /// the type.
83 /// @brief Get a ConstantInt for a specific value.
7484 ConstantInt* getConstantInt(const IntegerType* Ty, uint64_t V,
7585 bool isSigned = false);
86
87 /// Return a ConstantInt with the specified value for the specified type. The
88 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
89 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
90 /// signed value for the type Ty.
91 /// @brief Get a ConstantInt for a specific signed value.
7692 ConstantInt* getConstantIntSigned(const IntegerType* Ty, int64_t V);
93 Constant *getConstantIntSigned(const Type *Ty, int64_t V);
94
7795 ConstantInt* getConstantInt(const APInt& V);
96
97 /// If Ty is a vector type, return a Constant with a splat of the given
98 /// value. Otherwise return a ConstantInt for the given value.
7899 Constant* getConstantInt(const Type* Ty, const APInt& V);
79100
80101 // ConstantPointerNull accessors
96117 const std::vector& V);
97118 Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
98119 unsigned NumVals);
120
121 /// This method constructs a ConstantArray and initializes it with a text
122 /// string. The default behavior (AddNull==true) causes a null terminator to
123 /// be placed at the end of the array. This effectively increases the length
124 /// of the array by one (you've been warned). However, in some situations
125 /// this is not desired so if AddNull==false then the string is copied without
126 /// null termination.
99127 Constant* getConstantArray(const std::string& Initializer,
100128 bool AddNull = true);
101129
329329
330330 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
331331 const char *Name = "") {
332 return Insert(new MallocInst(Ty, ArraySize), Name);
332 return Insert(new MallocInst(Context, Ty, ArraySize), Name);
333333 }
334334 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
335335 const char *Name = "") {
336 return Insert(new AllocaInst(Ty, ArraySize), Name);
336 return Insert(new AllocaInst(Context, Ty, ArraySize), Name);
337337 }
338338 FreeInst *CreateFree(Value *Ptr) {
339339 return Insert(new FreeInst(Ptr));
2626 class AllocaInst;
2727 class ConstantExpr;
2828 class TargetData;
29 class LLVMContext;
2930 struct DbgInfoIntrinsic;
3031
3132 template class SmallVectorImpl;
106107 /// invalidating the SSA information for the value. It returns the pointer to
107108 /// the alloca inserted to create a stack slot for X.
108109 ///
109 AllocaInst *DemoteRegToStack(Instruction &X, bool VolatileLoads = false,
110 AllocaInst *DemoteRegToStack(LLVMContext &Context, Instruction &X,
111 bool VolatileLoads = false,
110112 Instruction *AllocaPoint = 0);
111113
112114 /// DemotePHIToStack - This function takes a virtual register computed by a phi
113115 /// node and replaces it with a slot in the stack frame, allocated via alloca.
114116 /// The phi node is deleted and it returns the pointer to the alloca inserted.
115 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
117 AllocaInst *DemotePHIToStack(LLVMContext &Context, PHINode *P,
118 Instruction *AllocaPoint = 0);
116119
117120 /// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used
118121 /// by DbgIntrinsics. If DbgInUses is specified then the vector is filled
195195
196196 const SCEV *
197197 ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
198 return getConstant(ConstantInt::get(cast(Ty), V, isSigned));
198 return getConstant(
199 Context->getConstantInt(cast(Ty), V, isSigned));
199200 }
200201
201202 const Type *SCEVConstant::getType() const { return V->getType(); }
21142115 /// specified signed integer value and return a SCEV for the constant.
21152116 const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
21162117 const IntegerType *ITy = cast(getEffectiveSCEVType(Ty));
2117 return getConstant(ConstantInt::get(ITy, Val));
2118 return getConstant(Context->getConstantInt(ITy, Val));
21182119 }
21192120
21202121 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
35363537
35373538 unsigned MaxSteps = MaxBruteForceIterations;
35383539 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
3539 ConstantInt *ItCst =
3540 ConstantInt::get(cast(IdxExpr->getType()), IterationNum);
3540 ConstantInt *ItCst = Context->getConstantInt(
3541 cast(IdxExpr->getType()), IterationNum);
35413542 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
35423543
35433544 // Form the GEP offset.
31813181 return Error(SizeLoc, "element count must be i32");
31823182
31833183 if (Opc == Instruction::Malloc)
3184 Inst = new MallocInst(Ty, Size, Alignment);
3184 Inst = new MallocInst(Context, Ty, Size, Alignment);
31853185 else
3186 Inst = new AllocaInst(Ty, Size, Alignment);
3186 Inst = new AllocaInst(Context, Ty, Size, Alignment);
31873187 return false;
31883188 }
31893189
18161816 Value *Size = getFnValueByID(Record[1], Type::Int32Ty);
18171817 unsigned Align = Record[2];
18181818 if (!Ty || !Size) return Error("Invalid MALLOC record");
1819 I = new MallocInst(Ty->getElementType(), Size, (1 << Align) >> 1);
1819 I = new MallocInst(Context, Ty->getElementType(), Size,
1820 (1 << Align) >> 1);
18201821 break;
18211822 }
18221823 case bitc::FUNC_CODE_INST_FREE: { // FREE: [op, opty]
18361837 Value *Size = getFnValueByID(Record[1], Type::Int32Ty);
18371838 unsigned Align = Record[2];
18381839 if (!Ty || !Size) return Error("Invalid ALLOCA record");
1839 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
1840 I = new AllocaInst(Context, Ty->getElementType(), Size,
1841 (1 << Align) >> 1);
18401842 break;
18411843 }
18421844 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
353353
354354 // Create the temporary if we didn't already.
355355 if (!ExceptionValueVar) {
356 ExceptionValueVar = new AllocaInst(PointerType::getUnqual(Type::Int8Ty),
356 ExceptionValueVar = new AllocaInst(*Context,
357 PointerType::getUnqual(Type::Int8Ty),
357358 "eh.value", F->begin()->begin());
358359 ++NumStackTempsIntroduced;
359360 }
148148
149149 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
150150 /// instruction IP.
151 static Value *LowerBSWAP(Value *V, Instruction *IP) {
151 static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
152152 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
153153
154154 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
158158 switch(BitSize) {
159159 default: llvm_unreachable("Unhandled type size of value to byteswap!");
160160 case 16: {
161 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
161 Value *Tmp1 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
162162 "bswap.2");
163 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
163 Value *Tmp2 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
164164 "bswap.1");
165165 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
166166 break;
167167 }
168168 case 32: {
169 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
169 Value *Tmp4 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 24),
170170 "bswap.4");
171 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
171 Value *Tmp3 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
172172 "bswap.3");
173 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
173 Value *Tmp2 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
174174 "bswap.2");
175 Value *Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
175 Value *Tmp1 = Builder.CreateLShr(V,Context.getConstantInt(V->getType(), 24),
176176 "bswap.1");
177 Tmp3 = Builder.CreateAnd(Tmp3, ConstantInt::get(Type::Int32Ty, 0xFF0000),
177 Tmp3 = Builder.CreateAnd(Tmp3,
178 Context.getConstantInt(Type::Int32Ty, 0xFF0000),
178179 "bswap.and3");
179 Tmp2 = Builder.CreateAnd(Tmp2, ConstantInt::get(Type::Int32Ty, 0xFF00),
180 Tmp2 = Builder.CreateAnd(Tmp2,
181 Context.getConstantInt(Type::Int32Ty, 0xFF00),
180182 "bswap.and2");
181183 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
182184 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
184186 break;
185187 }
186188 case 64: {
187 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
189 Value *Tmp8 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 56),
188190 "bswap.8");
189 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
191 Value *Tmp7 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 40),
190192 "bswap.7");
191 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
193 Value *Tmp6 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 24),
192194 "bswap.6");
193 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
195 Value *Tmp5 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
194196 "bswap.5");
195 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
197 Value* Tmp4 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
196198 "bswap.4");
197 Value* Tmp3 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
199 Value* Tmp3 = Builder.CreateLShr(V,
200 Context.getConstantInt(V->getType(), 24),
198201 "bswap.3");
199 Value* Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 40),
202 Value* Tmp2 = Builder.CreateLShr(V,
203 Context.getConstantInt(V->getType(), 40),
200204 "bswap.2");
201 Value* Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 56),
205 Value* Tmp1 = Builder.CreateLShr(V,
206 Context.getConstantInt(V->getType(), 56),
202207 "bswap.1");
203208 Tmp7 = Builder.CreateAnd(Tmp7,
204 ConstantInt::get(Type::Int64Ty,
209 Context.getConstantInt(Type::Int64Ty,
205210 0xFF000000000000ULL),
206211 "bswap.and7");
207212 Tmp6 = Builder.CreateAnd(Tmp6,
208 ConstantInt::get(Type::Int64Ty,
213 Context.getConstantInt(Type::Int64Ty,
209214 0xFF0000000000ULL),
210215 "bswap.and6");
211216 Tmp5 = Builder.CreateAnd(Tmp5,
212 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
217 Context.getConstantInt(Type::Int64Ty, 0xFF00000000ULL),
213218 "bswap.and5");
214219 Tmp4 = Builder.CreateAnd(Tmp4,
215 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
220 Context.getConstantInt(Type::Int64Ty, 0xFF000000ULL),
216221 "bswap.and4");
217222 Tmp3 = Builder.CreateAnd(Tmp3,
218 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
223 Context.getConstantInt(Type::Int64Ty, 0xFF0000ULL),
219224 "bswap.and3");
220225 Tmp2 = Builder.CreateAnd(Tmp2,
221 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
226 Context.getConstantInt(Type::Int64Ty, 0xFF00ULL),
222227 "bswap.and2");
223228 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
224229 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
235240
236241 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
237242 /// instruction IP.
238 static Value *LowerCTPOP(Value *V, Instruction *IP) {
243 static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
239244 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
240245
241246 static const uint64_t MaskValues[6] = {
248253
249254 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
250255 unsigned WordSize = (BitSize + 63) / 64;
251 Value *Count = ConstantInt::get(V->getType(), 0);
256 Value *Count = Context.getConstantInt(V->getType(), 0);
252257
253258 for (unsigned n = 0; n < WordSize; ++n) {
254259 Value *PartValue = V;
255260 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
256261 i <<= 1, ++ct) {
257 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
262 Value *MaskCst = Context.getConstantInt(V->getType(), MaskValues[ct]);
258263 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
259264 Value *VShift = Builder.CreateLShr(PartValue,
260 ConstantInt::get(V->getType(), i),
265 Context.getConstantInt(V->getType(), i),
261266 "ctpop.sh");
262267 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
263268 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
264269 }
265270 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
266271 if (BitSize > 64) {
267 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
272 V = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 64),
268273 "ctpop.part.sh");
269274 BitSize -= 64;
270275 }
275280
276281 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
277282 /// instruction IP.
278 static Value *LowerCTLZ(Value *V, Instruction *IP) {
283 static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
279284
280285 IRBuilder<> Builder(IP->getParent(), IP);
281286
282287 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
283288 for (unsigned i = 1; i < BitSize; i <<= 1) {
284 Value *ShVal = ConstantInt::get(V->getType(), i);
289 Value *ShVal = Context.getConstantInt(V->getType(), i);
285290 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
286291 V = Builder.CreateOr(V, ShVal, "ctlz.step");
287292 }
288293
289294 V = Builder.CreateNot(V);
290 return LowerCTPOP(V, IP);
295 return LowerCTPOP(Context, V, IP);
291296 }
292297
293298 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
356361 break;
357362 }
358363 case Intrinsic::ctpop:
359 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
364 CI->replaceAllUsesWith(LowerCTPOP(*Context, CI->getOperand(1), CI));
360365 break;
361366
362367 case Intrinsic::bswap:
363 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
368 CI->replaceAllUsesWith(LowerBSWAP(*Context, CI->getOperand(1), CI));
364369 break;
365370
366371 case Intrinsic::ctlz:
367 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
372 CI->replaceAllUsesWith(LowerCTLZ(*Context, CI->getOperand(1), CI));
368373 break;
369374
370375 case Intrinsic::cttz: {
372377 Value *Src = CI->getOperand(1);
373378 Value *NotSrc = Builder.CreateNot(Src);
374379 NotSrc->setName(Src->getName() + ".not");
375 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
380 Value *SrcM1 = Context->getConstantInt(Src->getType(), 1);
376381 SrcM1 = Builder.CreateSub(Src, SrcM1);
377 Src = LowerCTPOP(Builder.CreateAnd(NotSrc, SrcM1), CI);
382 Src = LowerCTPOP(*Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
378383 CI->replaceAllUsesWith(Src);
379384 break;
380385 }
408413 case Intrinsic::readcyclecounter: {
409414 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
410415 << "ter intrinsic. It is being lowered to a constant 0\n";
411 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
416 CI->replaceAllUsesWith(Context->getConstantInt(Type::Int64Ty, 0));
412417 break;
413418 }
414419
428433 case Intrinsic::eh_typeid_for_i32:
429434 case Intrinsic::eh_typeid_for_i64:
430435 // Return something different to eh_selector.
431 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
436 CI->replaceAllUsesWith(Context->getConstantInt(CI->getType(), 1));
432437 break;
433438
434439 case Intrinsic::var_annotation:
500505 case Intrinsic::flt_rounds:
501506 // Lower to "round to the nearest"
502507 if (CI->getType() != Type::VoidTy)
503 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
508 CI->replaceAllUsesWith(Context->getConstantInt(CI->getType(), 1));
504509 break;
505510 }
506511
2929 #include "llvm/DerivedTypes.h"
3030 #include "llvm/Function.h"
3131 #include "llvm/GlobalVariable.h"
32 #include "llvm/LLVMContext.h"
3233 #include "llvm/Support/CommandLine.h"
3334 #include "llvm/Support/Compiler.h"
3435 #include "llvm/Support/ErrorHandling.h"
20342035 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
20352036 }
20362037 if (TLI.isLittleEndian()) FF <<= 32;
2037 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
2038 Constant *FudgeFactor = DAG.getContext()->getConstantInt(Type::Int64Ty, FF);
20382039
20392040 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
20402041 unsigned Alignment = cast(CPIdx)->getAlignment();
6161 Constant *GetFrameMap(Function &F);
6262 const Type* GetConcreteStackEntryType(Function &F);
6363 void CollectRoots(Function &F);
64 static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr,
64 static GetElementPtrInst *CreateGEP(LLVMContext *Context,
65 IRBuilder<> &B, Value *BasePtr,
6566 int Idx1, const char *Name);
66 static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr,
67 static GetElementPtrInst *CreateGEP(LLVMContext *Context,
68 IRBuilder<> &B, Value *BasePtr,
6769 int Idx1, int Idx2, const char *Name);
6870 };
6971
185187
186188 Constant *ShadowStackGC::GetFrameMap(Function &F) {
187189 // doInitialization creates the abstract type of this value.
190 LLVMContext *Context = F.getContext();
188191
189192 Type *VoidPtr = PointerType::getUnqual(Type::Int8Ty);
190193
199202 }
200203
201204 Constant *BaseElts[] = {
202 ConstantInt::get(Type::Int32Ty, Roots.size(), false),
203 ConstantInt::get(Type::Int32Ty, NumMeta, false),
205 Context->getConstantInt(Type::Int32Ty, Roots.size(), false),
206 Context->getConstantInt(Type::Int32Ty, NumMeta, false),
204207 };
205208
206209 Constant *DescriptorElts[] = {
207 ConstantStruct::get(BaseElts, 2),
208 ConstantArray::get(ArrayType::get(VoidPtr, NumMeta),
210 Context->getConstantStruct(BaseElts, 2),
211 Context->getConstantArray(Context->getArrayType(VoidPtr, NumMeta),
209212 Metadata.begin(), NumMeta)
210213 };
211214
212 Constant *FrameMap = ConstantStruct::get(DescriptorElts, 2);
215 Constant *FrameMap = Context->getConstantStruct(DescriptorElts, 2);
213216
214217 std::string TypeName("gc_map.");
215218 TypeName += utostr(NumMeta);
232235 GlobalVariable::InternalLinkage,
233236 FrameMap, "__gc_" + F.getName());
234237
235 Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0),
236 ConstantInt::get(Type::Int32Ty, 0) };
237 return ConstantExpr::getGetElementPtr(GV, GEPIndices, 2);
238 Constant *GEPIndices[2] = { Context->getConstantInt(Type::Int32Ty, 0),
239 Context->getConstantInt(Type::Int32Ty, 0) };
240 return Context->getConstantExprGetElementPtr(GV, GEPIndices, 2);
238241 }
239242
240243 const Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) {
336339 }
337340
338341 GetElementPtrInst *
339 ShadowStackGC::CreateGEP(IRBuilder<> &B, Value *BasePtr,
342 ShadowStackGC::CreateGEP(LLVMContext *Context, IRBuilder<> &B, Value *BasePtr,
340343 int Idx, int Idx2, const char *Name) {
341 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
342 ConstantInt::get(Type::Int32Ty, Idx),
343 ConstantInt::get(Type::Int32Ty, Idx2) };
344 Value *Indices[] = { Context->getConstantInt(Type::Int32Ty, 0),
345 Context->getConstantInt(Type::Int32Ty, Idx),
346 Context->getConstantInt(Type::Int32Ty, Idx2) };
344347 Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name);
345348
346349 assert(isa(Val) && "Unexpected folded constant");
349352 }
350353
351354 GetElementPtrInst *
352 ShadowStackGC::CreateGEP(IRBuilder<> &B, Value *BasePtr,
355 ShadowStackGC::CreateGEP(LLVMContext *Context, IRBuilder<> &B, Value *BasePtr,
353356 int Idx, const char *Name) {
354 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
355 ConstantInt::get(Type::Int32Ty, Idx) };
357 Value *Indices[] = { Context->getConstantInt(Type::Int32Ty, 0),
358 Context->getConstantInt(Type::Int32Ty, Idx) };
356359 Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name);
357360
358361 assert(isa(Val) && "Unexpected folded constant");
362365
363366 /// runOnFunction - Insert code to maintain the shadow stack.
364367 bool ShadowStackGC::performCustomLowering(Function &F) {
368 LLVMContext *Context = F.getContext();
369
365370 // Find calls to llvm.gcroot.
366371 CollectRoots(F);
367372
386391
387392 // Initialize the map pointer and load the current head of the shadow stack.
388393 Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead");
389 Instruction *EntryMapPtr = CreateGEP(AtEntry, StackEntry,0,1,"gc_frame.map");
394 Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, StackEntry,
395 0,1,"gc_frame.map");
390396 AtEntry.CreateStore(FrameMap, EntryMapPtr);
391397
392398 // After all the allocas...
393399 for (unsigned I = 0, E = Roots.size(); I != E; ++I) {
394400 // For each root, find the corresponding slot in the aggregate...
395 Value *SlotPtr = CreateGEP(AtEntry, StackEntry, 1 + I, "gc_root");
401 Value *SlotPtr = CreateGEP(Context, AtEntry, StackEntry, 1 + I, "gc_root");
396402
397403 // And use it in lieu of the alloca.
398404 AllocaInst *OriginalAlloca = Roots[I].second;
408414 AtEntry.SetInsertPoint(IP->getParent(), IP);
409415
410416 // Push the entry onto the shadow stack.
411 Instruction *EntryNextPtr = CreateGEP(AtEntry,StackEntry,0,0,"gc_frame.next");
412 Instruction *NewHeadVal = CreateGEP(AtEntry,StackEntry, 0, "gc_newhead");
413 AtEntry.CreateStore(CurrentHead, EntryNextPtr);
414 AtEntry.CreateStore(NewHeadVal, Head);
417 Instruction *EntryNextPtr = CreateGEP(Context, AtEntry,
418 StackEntry,0,0,"gc_frame.next");
419 Instruction *NewHeadVal = CreateGEP(Context, AtEntry,
420 StackEntry, 0, "gc_newhead");
421 AtEntry.CreateStore(CurrentHead, EntryNextPtr);
422 AtEntry.CreateStore(NewHeadVal, Head);
415423
416424 // For each instruction that escapes...
417425 EscapeEnumerator EE(F, "gc_cleanup");
418426 while (IRBuilder<> *AtExit = EE.Next()) {
419427 // Pop the entry from the shadow stack. Don't reuse CurrentHead from
420428 // AtEntry, since that would make the value live for the entire function.
421 Instruction *EntryNextPtr2 = CreateGEP(*AtExit, StackEntry, 0, 0,
429 Instruction *EntryNextPtr2 = CreateGEP(Context, *AtExit, StackEntry, 0, 0,
422430 "gc_frame.next");
423431 Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead");
424432 AtExit->CreateStore(SavedHead, Head);
153153 BasicBlock &Entry = F->getEntryBlock();
154154 Instruction *InsPt = &Entry.front();
155155
156 AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
156 AI = new AllocaInst(*Context, PtrTy, "StackGuardSlot", InsPt);
157157 LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
158158
159159 Value *Args[] = { LI, AI };
350350 GenericValue JIT::runFunction(Function *F,
351351 const std::vector &ArgValues) {
352352 assert(F && "Function *F was null at entry to run()");
353 LLVMContext *Context = F->getContext();
353354
354355 void *FPtr = getPointerToFunction(F);
355356 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
451452 // arguments. Make this function and return.
452453
453454 // First, create the function.
454 FunctionType *STy=FunctionType::get(RetTy, false);
455 FunctionType *STy=Context->getFunctionType(RetTy, false);
455456 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
456457 F->getParent());
457458
468469 switch (ArgTy->getTypeID()) {
469470 default: llvm_unreachable("Unknown argument type for function call!");
470471 case Type::IntegerTyID:
471 C = ConstantInt::get(AV.IntVal);
472 C = Context->getConstantInt(AV.IntVal);
472473 break;
473474 case Type::FloatTyID:
474 C = ConstantFP::get(APFloat(AV.FloatVal));
475 C = Context->getConstantFP(APFloat(AV.FloatVal));
475476 break;
476477 case Type::DoubleTyID:
477 C = ConstantFP::get(APFloat(AV.DoubleVal));
478 C = Context->getConstantFP(APFloat(AV.DoubleVal));
478479 break;
479480 case Type::PPC_FP128TyID:
480481 case Type::X86_FP80TyID:
481482 case Type::FP128TyID:
482 C = ConstantFP::get(APFloat(AV.IntVal));
483 C = Context->getConstantFP(APFloat(AV.IntVal));
483484 break;
484485 case Type::PointerTyID:
485486 void *ArgPtr = GVTOP(AV);
486487 if (sizeof(void*) == 4)
487 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
488 C = Context->getConstantInt(Type::Int32Ty, (int)(intptr_t)ArgPtr);
488489 else
489 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
490 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
490 C = Context->getConstantInt(Type::Int64Ty, (intptr_t)ArgPtr);
491 // Cast the integer to pointer
492 C = Context->getConstantExprIntToPtr(C, ArgTy);
491493 break;
492494 }
493495 Args.push_back(C);
1919 #include "ARMSubtarget.h"
2020 #include "llvm/Constants.h"
2121 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/LLVMContext.h"
2224 #include "llvm/CodeGen/MachineConstantPool.h"
2325 #include "llvm/CodeGen/MachineFrameInfo.h"
2426 #include "llvm/CodeGen/MachineFunction.h"
889891 unsigned PredReg) const {
890892 MachineFunction &MF = *MBB.getParent();
891893 MachineConstantPool *ConstantPool = MF.getConstantPool();
892 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
894 Constant *C =
895 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
893896 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
894897
895898 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp), DestReg)
2020 #include "llvm/DerivedTypes.h"
2121 #include "llvm/Function.h"
2222 #include "llvm/Intrinsics.h"
23 #include "llvm/LLVMContext.h"
2324 #include "llvm/CodeGen/MachineFrameInfo.h"
2425 #include "llvm/CodeGen/MachineFunction.h"
2526 #include "llvm/CodeGen/MachineInstrBuilder.h"
864865 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
865866 if (UseCP) {
866867 SDValue CPIdx =
867 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
868 CurDAG->getTargetConstantPool(
869 CurDAG->getContext()->getConstantInt(Type::Int32Ty, Val),
868870 TLI.getPointerTy());
869871
870872 SDNode *ResNode;
1919 #include "Thumb1RegisterInfo.h"
2020 #include "llvm/Constants.h"
2121 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/LLVMContext.h"
2224 #include "llvm/CodeGen/MachineConstantPool.h"
2325 #include "llvm/CodeGen/MachineFrameInfo.h"
2426 #include "llvm/CodeGen/MachineFunction.h"
6466 unsigned PredReg) const {
6567 MachineFunction &MF = *MBB.getParent();
6668 MachineConstantPool *ConstantPool = MF.getConstantPool();
67 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
69 Constant *C =
70 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
6871 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
6972
7073 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRcp), DestReg)
1919 #include "Thumb2RegisterInfo.h"
2020 #include "llvm/Constants.h"
2121 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/LLVMContext.h"
2224 #include "llvm/CodeGen/MachineConstantPool.h"
2325 #include "llvm/CodeGen/MachineFrameInfo.h"
2426 #include "llvm/CodeGen/MachineFunction.h"
4850 unsigned PredReg) const {
4951 MachineFunction &MF = *MBB.getParent();
5052 MachineConstantPool *ConstantPool = MF.getConstantPool();
51 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
53 Constant *C =
54 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
5255 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
5356
5457 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2LDRpci), DestReg)
2525 #include "llvm/DerivedTypes.h"
2626 #include "llvm/GlobalValue.h"
2727 #include "llvm/Intrinsics.h"
28 #include "llvm/LLVMContext.h"
2829 #include "llvm/Support/Compiler.h"
2930 #include "llvm/Support/Debug.h"
3031 #include "llvm/Support/ErrorHandling.h"
303304 // val32 >= IMM_LOW + IMM_LOW * IMM_MULT) //always true
304305 break; //(zext (LDAH (LDA)))
305306 //Else use the constant pool
306 ConstantInt *C = ConstantInt::get(Type::Int64Ty, uval);
307 ConstantInt *C = CurDAG->getContext()->getConstantInt(Type::Int64Ty, uval);
307308 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
308309 SDNode *Tmp = CurDAG->getTargetNode(Alpha::LDAHr, dl, MVT::i64, CPI,
309310 SDValue(getGlobalBaseReg(), 0));
35023502 if (!ITy->isPowerOf2ByteWidth())
35033503 // We have a bit width that doesn't match an even power-of-2 byte
35043504 // size. Consequently we must & the value with the type's bit mask
3505 BitMask = ConstantInt::get(ITy, ITy->getBitMask());
3505 BitMask = Context->getConstantInt(ITy, ITy->getBitMask());
35063506 if (BitMask)
35073507 Out << "((";
35083508 writeOperand(Operand);
1818 #include "llvm/Intrinsics.h"
1919 #include "llvm/CallingConv.h"
2020 #include "llvm/Constants.h"
21 #include "llvm/LLVMContext.h"
2122 #include "llvm/CodeGen/MachineFrameInfo.h"
2223 #include "llvm/CodeGen/MachineFunction.h"
2324 #include "llvm/CodeGen/MachineInstrBuilder.h"
172173 else if (! Predicate_immU16(N)) {
173174 unsigned Val = cast(N)->getZExtValue();
174175 SDValue CPIdx =
175 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
176 CurDAG->getTargetConstantPool(
177 CurDAG->getContext()->getConstantInt(Type::Int32Ty, Val),
176178 TLI.getPointerTy());
177179 return CurDAG->getTargetNode(XCore::LDWCP_lru6, dl, MVT::i32,
178180 MVT::Other, CPIdx,
754754
755755 // Just add all the struct element types.
756756 const Type *AgTy = cast(I->getType())->getElementType();
757 Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
757 Value *TheAlloca = new AllocaInst(*Context, AgTy, 0, "", InsertPt);
758758 const StructType *STy = cast(AgTy);
759759 Value *Idxs[2] = { Context->getConstantInt(Type::Int32Ty, 0), 0 };
760760
827827 Type *NewTy = Context->getArrayType(MI->getAllocatedType(),
828828 NElements->getZExtValue());
829829 MallocInst *NewMI =
830 new MallocInst(NewTy, Context->getNullValue(Type::Int32Ty),
830 new MallocInst(*Context, NewTy, Context->getNullValue(Type::Int32Ty),
831831 MI->getAlignment(), MI->getName(), MI);
832832 Value* Indices[2];
833833 Indices[0] = Indices[1] = Context->getNullValue(Type::Int32Ty);
12901290 GV->isThreadLocal());
12911291 FieldGlobals.push_back(NGV);
12921292
1293 MallocInst *NMI = new MallocInst(FieldTy, MI->getArraySize(),
1293 MallocInst *NMI = new MallocInst(*Context, FieldTy, MI->getArraySize(),
12941294 MI->getName() + ".f" + utostr(FieldNo),MI);
12951295 FieldMallocs.push_back(NMI);
12961296 new StoreInst(NMI, NGV, MI);
15061506 // structs. malloc [100 x struct],1 -> malloc struct, 100
15071507 if (const ArrayType *AT = dyn_cast(MI->getAllocatedType())) {
15081508 MallocInst *NewMI =
1509 new MallocInst(AllocSTy,
1509 new MallocInst(*Context, AllocSTy,
15101510 Context->getConstantInt(Type::Int32Ty, AT->getNumElements()),
15111511 "", MI);
15121512 NewMI->takeName(MI);
17021702 Instruction* FirstI = GS.AccessingFunction->getEntryBlock().begin();
17031703 const Type* ElemTy = GV->getType()->getElementType();
17041704 // FIXME: Pass Global's alignment when globals have alignment
1705 AllocaInst* Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), FirstI);
1705 AllocaInst* Alloca = new AllocaInst(*Context, ElemTy, NULL,
1706 GV->getName(), FirstI);
17061707 if (!isa(GV->getInitializer()))
17071708 new StoreInst(GV->getInitializer(), Alloca, FirstI);
17081709
None //===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
0 //===-- IndMemRemoval.cpp - Remove indirect allocations and frees ---------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
99 // This pass finds places where memory allocation functions may escape into
1010 // indirect land. Some transforms are much easier (aka possible) only if free
1111 // or malloc are not called indirectly.
12 // Thus find places where the address of memory functions are taken and construct
13 // bounce functions with direct calls of those functions.
12 // Thus find places where the address of memory functions are taken and
13 // construct bounce functions with direct calls of those functions.
1414 //
1515 //===----------------------------------------------------------------------===//
1616
7272 BasicBlock* bb = BasicBlock::Create("entry",FN);
7373 Instruction* c = CastInst::CreateIntegerCast(
7474 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
75 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
75 Instruction* a = new MallocInst(*Context, Type::Int8Ty, c, "m", bb);
7676 ReturnInst::Create(a, bb);
7777 ++NumBounce;
7878 NumBounceSites += F->getNumUses();
310310
311311 // Fill in the alloca and call to initialize the SJ map.
312312 const Type *SBPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
313 AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
313 AllocaInst* Map = new AllocaInst(*Context, SBPTy, 0, "SJMap", Inst);
314314 CallInst::Create(InitSJMap, Map, "", Inst);
315315 return SJMap[Func] = Map;
316316 }
407407 UI != E; ++UI)
408408 if (cast(*UI)->getParent() != ABlock ||
409409 InstrsAfterCall.count(cast(*UI))) {
410 DemoteRegToStack(*II);
410 DemoteRegToStack(*Context, *II);
411411 break;
412412 }
413413 InstrsAfterCall.clear();
168168 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
169169 "MallocAmtCast", I);
170170
171 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
171 MallocInst *MI = new MallocInst(*Context, Type::Int8Ty,
172 Source, "", I);
172173 MI->takeName(I);
173174 I->replaceAllUsesWith(MI);
174175
109109 DOUT << "SretPromotion: sret argument will be promoted\n";
110110 NumSRET++;
111111 // [1] Replace use of sret parameter
112 AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv",
112 AllocaInst *TheAlloca = new AllocaInst (*Context, STy, NULL, "mrv",
113113 F->getEntryBlock().begin());
114114 Value *NFirstArg = F->arg_begin();
115115 NFirstArg->replaceAllUsesWith(TheAlloca);
247247 //make a local temporary to cache the global
248248 BasicBlock& bb = F->getEntryBlock();
249249 BasicBlock::iterator InsertPt = bb.begin();
250 AI = new AllocaInst(T, 0, "localcounter", InsertPt);
250 AI = new AllocaInst(*F->getContext(), T, 0, "localcounter", InsertPt);
251251 LoadInst* l = new LoadInst(Counter, "counterload", InsertPt);
252252 new StoreInst(l, AI, InsertPt);
253253
17551755 Value *LHS = II->getOperand(1);
17561756 Value *RHS = II->getOperand(2);
17571757 // Extract the element as scalars.
1758 LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II);
1759 RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II);
1758 LHS = InsertNewInstBefore(new ExtractElementInst(LHS,
1759 Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
1760 RHS = InsertNewInstBefore(new ExtractElementInst(RHS,
1761 Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
17601762
17611763 switch (II->getIntrinsicID()) {
17621764 default: llvm_unreachable("Case stmts out of sync!");
17741776
17751777 Instruction *New =
17761778 InsertElementInst::Create(
1777 Context->getUndef(II->getType()), TmpV, 0U, II->getName());
1779 Context->getUndef(II->getType()), TmpV,
1780 Context->getConstantInt(Type::Int32Ty, 0U, false), II->getName());
17781781 InsertNewInstBefore(New, *II);
17791782 AddSoonDeadInstToWorklist(*II, 0);
17801783 return New;
78877890
78887891 AllocationInst *New;
78897892 if (isa(AI))
7890 New = new MallocInst(CastElTy, Amt, AI.getAlignment());
7893 New = new MallocInst(*Context, CastElTy, Amt, AI.getAlignment());
78917894 else
7892 New = new AllocaInst(CastElTy, Amt, AI.getAlignment());
7895 New = new AllocaInst(*Context, CastElTy, Amt, AI.getAlignment());
78937896 InsertNewInstBefore(New, AI);
78947897 New->takeName(&AI);
78957898
99739976
99749977 if (ExtractedElts[Idx] == 0) {
99759978 Instruction *Elt =
9976 new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp");
9979 new ExtractElementInst(Idx < 16 ? Op0 : Op1,
9980 Context->getConstantInt(Type::Int32Ty, Idx&15, false), "tmp");
99779981 InsertNewInstBefore(Elt, CI);
99789982 ExtractedElts[Idx] = Elt;
99799983 }
99809984
99819985 // Insert this value into the result vector.
99829986 Result = InsertElementInst::Create(Result, ExtractedElts[Idx],
9983 i, "tmp");
9987 Context->getConstantInt(Type::Int32Ty, i, false),
9988 "tmp");
99849989 InsertNewInstBefore(cast(Result), CI);
99859990 }
99869991 return CastInst::Create(Instruction::BitCast, Result, CI.getType());
1136211367
1136311368 // Create and insert the replacement instruction...
1136411369 if (isa(AI))
11365 New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName());
11370 New = new MallocInst(*Context, NewTy, 0,
11371 AI.getAlignment(), AI.getName());
1136611372 else {
1136711373 assert(isa(AI) && "Unknown type of allocation inst!");
11368 New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName());
11374 New = new AllocaInst(*Context, NewTy, 0,
11375 AI.getAlignment(), AI.getName());
1136911376 }
1137011377
1137111378 InsertNewInstBefore(New, AI);
1247412481 } else {
1247512482 return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
1247612483 }
12477 return new ExtractElementInst(Src, SrcIdx);
12484 return new ExtractElementInst(Src,
12485 Context->getConstantInt(Type::Int32Ty, SrcIdx, false));
1247812486 }
1247912487 }
1248012488 }
911911
912912 // We found a use of I outside of BB. Create a new stack slot to
913913 // break this inter-block usage pattern.
914 DemoteRegToStack(*I);
914 DemoteRegToStack(*Context, *I);
915915 }
916916
917917 // We are going to have to map operands from the original BB block to the new
507507 AllocaInst *AI = 0;
508508
509509 if (I.getType() != Type::VoidTy) {
510 AI = new AllocaInst(I.getType(), 0, I.getName(),
510 AI = new AllocaInst(*Context, I.getType(), 0, I.getName(),
511511 I.getParent()->getParent()->getEntryBlock().begin());
512512 CurAST->add(AI);
513513 }
852852 continue;
853853
854854 const Type *Ty = cast(V->getType())->getElementType();
855 AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart);
855 AllocaInst *AI = new AllocaInst(*Context, Ty, 0,
856 V->getName()+".tmp", FnStart);
856857 PromotedValues.push_back(std::make_pair(AI, V));
857858
858859 // Update the AST and alias analysis.
18901890 assert(!Ty->isFPOrFPVector() && "Float in work queue!");
18911891
18921892 Constant *Zero = Context->getNullValue(Ty);
1893 Constant *One = ConstantInt::get(Ty, 1);
1893 Constant *One = Context->getConstantInt(Ty, 1);
18941894 ConstantInt *AllOnes = cast(Context->getAllOnesValue(Ty));
18951895
18961896 switch (Opcode) {
8888 NumRegsDemoted += worklist.size();
8989 for (std::list::iterator ilb = worklist.begin(),
9090 ile = worklist.end(); ilb != ile; ++ilb)
91 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
91 DemoteRegToStack(*Context, **ilb, false, AllocaInsertionPoint);
9292
9393 worklist.clear();
9494
104104 NumPhisDemoted += worklist.size();
105105 for (std::list::iterator ilb = worklist.begin(),
106106 ile = worklist.end(); ilb != ile; ++ilb)
107 DemotePHIToStack(cast(*ilb), AllocaInsertionPoint);
107 DemotePHIToStack(*Context, cast(*ilb), AllocaInsertionPoint);
108108
109109 return true;
110110 }
301301 DOUT << "CONVERT TO VECTOR: " << *AI << " TYPE = " << *VectorTy <<"\n";
302302
303303 // Create and insert the vector alloca.
304 NewAI = new AllocaInst(VectorTy, 0, "", AI->getParent()->begin());
304 NewAI = new AllocaInst(*Context, VectorTy, 0, "",
305 AI->getParent()->begin());
305306 ConvertUsesToScalar(AI, NewAI, 0);
306307 } else {
307308 DOUT << "CONVERT TO SCALAR INTEGER: " << *AI << "\n";
308309
309310 // Create and insert the integer alloca.
310311 const Type *NewTy = Context->getIntegerType(AllocaSize*8);
311 NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin());
312 NewAI = new AllocaInst(*Context, NewTy, 0, "",
313 AI->getParent()->begin());
312314 ConvertUsesToScalar(AI, NewAI, 0);
313315 }
314316 NewAI->takeName(AI);
333335 if (const StructType *ST = dyn_cast(AI->getAllocatedType())) {
334336 ElementAllocas.reserve(ST->getNumContainedTypes());
335337 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
336 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
338 AllocaInst *NA = new AllocaInst(*Context,
339 ST->getContainedType(i), 0,
337340 AI->getAlignment(),
338341 AI->getName() + "." + utostr(i), AI);
339342 ElementAllocas.push_back(NA);
344347 ElementAllocas.reserve(AT->getNumElements());
345348 const Type *ElTy = AT->getElementType();
346349 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
347 AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(),
350 AllocaInst *NA = new AllocaInst(*Context, ElTy, 0, AI->getAlignment(),
348351 AI->getName() + "." + utostr(i), AI);
349352 ElementAllocas.push_back(NA);
350353 WorkList.push_back(NA); // Add to worklist for recursive processing
284284 if (I->isUsedOutsideOfBlock(DestBlock)) {
285285 // We found a use outside of the tail. Create a new stack slot to
286286 // break this inter-block usage pattern.
287 DemoteRegToStack(*I);
287 DemoteRegToStack(*Context, *I);
288288 }
289289
290290 // We are going to have to map operands from the original block B to the new
371371 StructValues.push_back(*i);
372372 } else {
373373 AllocaInst *alloca =
374 new AllocaInst((*i)->getType(), 0, (*i)->getName()+".loc",
374 new AllocaInst(*codeReplacer->getContext(),
375 (*i)->getType(), 0, (*i)->getName()+".loc",
375376 codeReplacer->getParent()->begin()->begin());
376377 ReloadOutputs.push_back(alloca);
377378 params.push_back(alloca);
388389 // Allocate a struct at the beginning of this function
389390 Type *StructArgTy = Context->getStructType(ArgTypes);
390391 Struct =
391 new AllocaInst(StructArgTy, 0, "structArg",
392 new AllocaInst(*codeReplacer->getContext(), StructArgTy, 0, "structArg",
392393 codeReplacer->getParent()->begin()->begin());
393394 params.push_back(Struct);
394395
2828 /// invalidating the SSA information for the value. It returns the pointer to
2929 /// the alloca inserted to create a stack slot for I.
3030 ///
31 AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
31 AllocaInst* llvm::DemoteRegToStack(LLVMContext &Context,
32 Instruction &I, bool VolatileLoads,
3233 Instruction *AllocaPoint) {
3334 if (I.use_empty()) {
3435 I.eraseFromParent();
3839 // Create a stack slot to hold the value.
3940 AllocaInst *Slot;
4041 if (AllocaPoint) {
41 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem", AllocaPoint);
42 Slot = new AllocaInst(Context, I.getType(), 0,
43 I.getName()+".reg2mem", AllocaPoint);
4244 } else {
4345 Function *F = I.getParent()->getParent();
44 Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
46 Slot = new AllocaInst(Context, I.getType(), 0, I.getName()+".reg2mem",
4547 F->getEntryBlock().begin());
4648 }
4749
106108 /// DemotePHIToStack - This function takes a virtual register computed by a phi
107109 /// node and replaces it with a slot in the stack frame, allocated via alloca.
108110 /// The phi node is deleted and it returns the pointer to the alloca inserted.
109 AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
111 AllocaInst* llvm::DemotePHIToStack(LLVMContext &Context, PHINode *P,
112 Instruction *AllocaPoint) {
110113 if (P->use_empty()) {
111114 P->eraseFromParent();
112115 return 0;
115118 // Create a stack slot to hold the value.
116119 AllocaInst *Slot;
117120 if (AllocaPoint) {
118 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem", AllocaPoint);
121 Slot = new AllocaInst(Context, P->getType(), 0,
122 P->getName()+".reg2mem", AllocaPoint);
119123 } else {
120124 Function *F = P->getParent()->getParent();
121 Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
125 Slot = new AllocaInst(Context, P->getType(), 0, P->getName()+".reg2mem",
122126 F->getEntryBlock().begin());
123127 }
124128
308308 // Create the alloca. If we have TargetData, use nice alignment.
309309 unsigned Align = 1;
310310 if (TD) Align = TD->getPrefTypeAlignment(AggTy);
311 Value *NewAlloca = new AllocaInst(AggTy, 0, Align, I->getName(),
312 Caller->begin()->begin());
311 Value *NewAlloca = new AllocaInst(*Context, AggTy, 0, Align,
312 I->getName(),
313 &*Caller->begin()->begin());
313314 // Emit a memcpy.
314315 const Type *Tys[] = { Type::Int64Ty };
315316 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
265265 void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
266266 AllocaInst *InvokeNum,
267267 SwitchInst *CatchSwitch) {
268 ConstantInt *InvokeNoC = ConstantInt::get(Type::Int32Ty, InvokeNo);
268 ConstantInt *InvokeNoC = Context->getConstantInt(Type::Int32Ty, InvokeNo);
269269
270270 // If the unwind edge has phi nodes, split the edge.
271271 if (isa(II->getUnwindDest()->begin())) {
416416 // If we decided we need a spill, do it.
417417 if (NeedsSpill) {
418418 ++NumSpilled;
419 DemoteRegToStack(*Inst, true);
419 DemoteRegToStack(*Context, *Inst, true);
420420 }
421421 }
422422 }
469469 // alloca because the value needs to be live across invokes.
470470 unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
471471 AllocaInst *JmpBuf =
472 new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
472 new AllocaInst(*Context, JBLinkTy, 0, Align,
473 "jblink", F.begin()->begin());
473474
474475 std::vector Idx;
475476 Idx.push_back(Context->getNullValue(Type::Int32Ty));
476 Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
477 Idx.push_back(Context->getConstantInt(Type::Int32Ty, 1));
477478 OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
478 "OldBuf", EntryBB->getTerminator());
479 "OldBuf",
480 EntryBB->getTerminator());
479481
480482 // Copy the JBListHead to the alloca.
481483 Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
491493
492494 // Create an alloca which keeps track of which invoke is currently
493495 // executing. For normal calls it contains zero.
494 AllocaInst *InvokeNum = new AllocaInst(Type::Int32Ty, 0, "invokenum",
495 EntryBB->begin());
496 new StoreInst(ConstantInt::get(Type::Int32Ty, 0), InvokeNum, true,
496 AllocaInst *InvokeNum = new AllocaInst(*Context, Type::Int32Ty, 0,
497 "invokenum",EntryBB->begin());
498 new StoreInst(Context->getConstantInt(Type::Int32Ty, 0), InvokeNum, true,
497499 EntryBB->getTerminator());
498500
499501 // Insert a load in the Catch block, and a switch on its value. By default,
512514 BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
513515 "setjmp.cont");
514516
515 Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
517 Idx[1] = Context->getConstantInt(Type::Int32Ty, 0);
516518 Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
517519 "TheJmpBuf",
518520 EntryBB->getTerminator());
566568 // Get a pointer to the jmpbuf and longjmp.
567569 std::vector Idx;
568570 Idx.push_back(Context->getNullValue(Type::Int32Ty));
569 Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
571 Idx.push_back(Context->getConstantInt(Type::Int32Ty, 0));
570572 Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
571573 UnwindBlock);
572574 Idx[0] = new BitCastInst(Idx[0], PointerType::getUnqual(Type::Int8Ty),
573575 "tmp", UnwindBlock);
574 Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
576 Idx[1] = Context->getConstantInt(Type::Int32Ty, 1);
575577 CallInst::Create(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
576578 new UnreachableInst(UnwindBlock);
577579
180180
181181 ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
182182 assert(TheTrueVal == 0 && TheFalseVal == 0);
183 TheTrueVal = get(Type::Int1Ty, 1);
184 TheFalseVal = get(Type::Int1Ty, 0);
183 TheTrueVal = getGlobalContext().getConstantInt(Type::Int1Ty, 1);
184 TheFalseVal = getGlobalContext().getConstantInt(Type::Int1Ty, 0);
185185
186186 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
187187 TrueFalseCleanup.Register();
222222 DenseMapAPIntKeyInfo> IntMapTy;
223223 static ManagedStatic IntConstants;
224224
225 ConstantInt *ConstantInt::get(const IntegerType *Ty,
226 uint64_t V, bool isSigned) {
227 return get(APInt(Ty->getBitWidth(), V, isSigned));
228 }
229
230 Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
231 Constant *C = get(cast(Ty->getScalarType()), V, isSigned);
232
233 // For vectors, broadcast the value.
234 if (const VectorType *VTy = dyn_cast(Ty))
235 return
236 ConstantVector::get(std::vector(VTy->getNumElements(), C));
237
238 return C;
239 }
240
241225 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
242226 // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
243227 // operator== and operator!= to ensure that the DenseMap doesn't attempt to
264248 } else {
265249 return Slot;
266250 }
267 }
268
269 Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
270 ConstantInt *C = ConstantInt::get(V);
271 assert(C->getType() == Ty->getScalarType() &&
272 "ConstantInt type doesn't match the type implied by its value!");
273
274 // For vectors, broadcast the value.
275 if (const VectorType *VTy = dyn_cast(Ty))
276 return
277 ConstantVector::get(std::vector(VTy->getNumElements(), C));
278
279 return C;
280251 }
281252
282253 //===----------------------------------------------------------------------===//
13041275 // Implicitly locked.
13051276 ArrayConstants->remove(this);
13061277 destroyConstantImpl();
1307 }
1308
1309 /// ConstantArray::get(const string&) - Return an array that is initialized to
1310 /// contain the specified string. If length is zero then a null terminator is
1311 /// added to the specified string so that it may be used in a natural way.
1312 /// Otherwise, the length parameter specifies how much of the string to use
1313 /// and it won't be null terminated.
1314 ///
1315 Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
1316 std::vector ElementVals;
1317 for (unsigned i = 0; i < Str.length(); ++i)
1318 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
1319
1320 // Add a null terminator to the string...
1321 if (AddNull) {
1322 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
1323 }
1324
1325 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
1326 return ConstantArray::get(ATy, ElementVals);
13271278 }
13281279
13291280 /// isString - This method returns true if the array is an array of i8, and
699699 // AllocationInst Implementation
700700 //===----------------------------------------------------------------------===//
701701
702 static Value *getAISize(Value *Amt) {
702 static Value *getAISize(LLVMContext &Context, Value *Amt) {
703703 if (!Amt)
704 Amt = ConstantInt::get(Type::Int32Ty, 1);
704 Amt = Context.getConstantInt(Type::Int32Ty, 1);
705705 else {
706706 assert(!isa(Amt) &&
707707 "Passed basic block into allocation size parameter! Use other ctor");
711711 return Amt;
712712 }
713713
714 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
714 AllocationInst::AllocationInst(LLVMContext &C,
715 const Type *Ty, Value *ArraySize, unsigned iTy,
715716 unsigned Align, const std::string &Name,
716717 Instruction *InsertBefore)
717 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
718 InsertBefore) {
718 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
719 getAISize(Context, ArraySize), InsertBefore),
720 Context(C) {
719721 setAlignment(Align);
720722 assert(Ty != Type::VoidTy && "Cannot allocate void!");
721723 setName(Name);
722724 }
723725
724 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
726 AllocationInst::AllocationInst(LLVMContext &C,
727 const Type *Ty, Value *ArraySize, unsigned iTy,
725728 unsigned Align, const std::string &Name,
726729 BasicBlock *InsertAtEnd)
727 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
728 InsertAtEnd) {
730 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
731 getAISize(Context, ArraySize), InsertAtEnd),
732 Context(C) {
729733 setAlignment(Align);
730734 assert(Ty != Type::VoidTy && "Cannot allocate void!");
731735 setName(Name);
752756 }
753757
754758 AllocaInst::AllocaInst(const AllocaInst &AI)
755 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
756 Instruction::Alloca, AI.getAlignment()) {
759 : AllocationInst(AI.Context, AI.getType()->getElementType(),
760 (Value*)AI.getOperand(0), Instruction::Alloca,
761 AI.getAlignment()) {
757762 }
758763
759764 /// isStaticAlloca - Return true if this alloca is in the entry block of the
769774 }
770775
771776 MallocInst::MallocInst(const MallocInst &MI)
772 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
773 Instruction::Malloc, MI.getAlignment()) {
777 : AllocationInst(MI.Context, MI.getType()->getElementType(),
778 (Value*)MI.getOperand(0), Instruction::Malloc,
779 MI.getAlignment()) {
774780 }
775781
776782 //===----------------------------------------------------------------------===//
11721178 setName(Name);
11731179 }
11741180
1175 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1176 const std::string &Name,
1177 Instruction *InsertBef)
1178 : Instruction(cast(Val->getType())->getElementType(),
1179 ExtractElement,
1180 OperandTraits::op_begin(this),
1181 2, InsertBef) {
1182 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1183 assert(isValidOperands(Val, Index) &&
1184 "Invalid extractelement instruction operands!");
1185 Op<0>() = Val;
1186 Op<1>() = Index;
1187 setName(Name);
1188 }
1189
1190
11911181 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
11921182 const std::string &Name,
11931183 BasicBlock *InsertAE)
11981188 assert(isValidOperands(Val, Index) &&
11991189 "Invalid extractelement instruction operands!");
12001190
1201 Op<0>() = Val;
1202 Op<1>() = Index;
1203 setName(Name);
1204 }
1205
1206 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1207 const std::string &Name,
1208 BasicBlock *InsertAE)
1209 : Instruction(cast(Val->getType())->getElementType(),
1210 ExtractElement,
1211 OperandTraits::op_begin(this),
1212 2, InsertAE) {
1213 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1214 assert(isValidOperands(Val, Index) &&
1215 "Invalid extractelement instruction operands!");
1216
12171191 Op<0>() = Val;
12181192 Op<1>() = Index;
12191193 setName(Name);
12521226 setName(Name);
12531227 }
12541228
1255 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1256 const std::string &Name,
1257 Instruction *InsertBef)
1258 : Instruction(Vec->getType(), InsertElement,
1259 OperandTraits::op_begin(this),
1260 3, InsertBef) {
1261 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1262 assert(isValidOperands(Vec, Elt, Index) &&
1263 "Invalid insertelement instruction operands!");
1264 Op<0>() = Vec;
1265 Op<1>() = Elt;
1266 Op<2>() = Index;
1267 setName(Name);
1268 }
1269
1270
12711229 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
12721230 const std::string &Name,
12731231 BasicBlock *InsertAE)
12771235 assert(isValidOperands(Vec, Elt, Index) &&
12781236 "Invalid insertelement instruction operands!");
12791237
1280 Op<0>() = Vec;
1281 Op<1>() = Elt;
1282 Op<2>() = Index;
1283 setName(Name);
1284 }
1285
1286 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1287 const std::string &Name,
1288 BasicBlock *InsertAE)
1289 : Instruction(Vec->getType(), InsertElement,
1290 OperandTraits::op_begin(this),
1291 3, InsertAE) {
1292 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
1293 assert(isValidOperands(Vec, Elt, Index) &&
1294 "Invalid insertelement instruction operands!");
1295
12961238 Op<0>() = Vec;
12971239 Op<1>() = Elt;
12981240 Op<2>() = Index;
8989
9090 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
9191 bool isSigned) {
92 return ConstantInt::get(Ty, V, isSigned);
92 Constant *C = getConstantInt(cast(Ty->getScalarType()),
93 V, isSigned);
94
95 // For vectors, broadcast the value.
96 if (const VectorType *VTy = dyn_cast(Ty))
97 return
98 getConstantVector(std::vector(VTy->getNumElements(), C));
99
100 return C;
93101 }
94102
95103
96104 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
97105 bool isSigned) {
98 return ConstantInt::get(Ty, V, isSigned);
106 return getConstantInt(APInt(Ty->getBitWidth(), V, isSigned));
99107 }
100108
101109 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
102110 int64_t V) {
103 return ConstantInt::getSigned(Ty, V);
111 return getConstantInt(Ty, V, true);
112 }
113
114 Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) {
115 return getConstantInt(Ty, V, true);
104116 }
105117
106118 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
108120 }
109121
110122 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
111 return ConstantInt::get(Ty, V);
123 ConstantInt *C = getConstantInt(V);
124 assert(C->getType() == Ty->getScalarType() &&
125 "ConstantInt type doesn't match the type implied by its value!");
126
127 // For vectors, broadcast the value.
128 if (const VectorType *VTy = dyn_cast(Ty))
129 return
130 ConstantVector::get(std::vector(VTy->getNumElements(), C));
131
132 return C;
112133 }
113134
114135 // ConstantPointerNull accessors.
152173 return ConstantArray::get(T, Vals, NumVals);
153174 }
154175
155 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
176 /// ConstantArray::get(const string&) - Return an array that is initialized to
177 /// contain the specified string. If length is zero then a null terminator is
178 /// added to the specified string so that it may be used in a natural way.
179 /// Otherwise, the length parameter specifies how much of the string to use
180 /// and it won't be null terminated.
181 ///
182 Constant* LLVMContext::getConstantArray(const std::string& Str,
156183 bool AddNull) {
157 return ConstantArray::get(Initializer, AddNull);
184 std::vector ElementVals;
185 for (unsigned i = 0; i < Str.length(); ++i)
186 ElementVals.push_back(getConstantInt(Type::Int8Ty, Str[i]));
187
188 // Add a null terminator to the string...
189 if (AddNull) {
190 ElementVals.push_back(getConstantInt(Type::Int8Ty, 0));
191 }
192
193 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
194 return getConstantArray(ATy, ElementVals);
158195 }
159196
160197
7373
7474 // If this instruction produces a value, replace any users with null values
7575 if (isa(TheInst->getType()))
76 TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType()));
76 TheInst->replaceAllUsesWith(Context.getUndef(TheInst->getType()));
7777 else if (TheInst->getType() != Type::VoidTy)
7878 TheInst->replaceAllUsesWith(Context.getNullValue(TheInst->getType()));
7979
182182 /// as a constant array.
183183 static Constant *GetTorInit(std::vector > &TorList) {
184184 assert(!TorList.empty() && "Don't create empty tor list!");
185 LLVMContext &Context = *TorList[0].first->getContext();
185186 std::vector ArrayElts;
186187 for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
187188 std::vector Elts;
188 Elts.push_back(ConstantInt::get(Type::Int32Ty, TorList[i].second));
189 Elts.push_back(Context.getConstantInt(Type::Int32Ty, TorList[i].second));
189190 Elts.push_back(TorList[i].first);
190 ArrayElts.push_back(ConstantStruct::get(Elts));
191 }
192 return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
191 ArrayElts.push_back(Context.getConstantStruct(Elts));
192 }
193 return Context.getConstantArray(Context.getArrayType(ArrayElts[0]->getType(),
193194 ArrayElts.size()),
194195 ArrayElts);
195196 }
6666 BD.EmitProgressBitcode("pass-error", false);
6767 exit(BD.debugOptimizerCrash());
6868 }
69
69
7070 // Check to see if the finished program matches the reference output...
7171 if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) {
7272 std::cout << " nope.\n";
639639 ///
640640 static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
641641 Module *Safe) {
642 LLVMContext &Context = BD.getContext();
643
642644 // Clean up the modules, removing extra cruft that we don't need anymore...
643645 Test = BD.performFinalCleanups(Test);
644646
688690 // Prototype: void *getPointerToNamedFunction(const char* Name)
689691 Constant *resolverFunc =
690692 Safe->getOrInsertFunction("getPointerToNamedFunction",
691 PointerType::getUnqual(Type::Int8Ty),
692 PointerType::getUnqual(Type::Int8Ty), (Type *)0);
693 Context.getPointerTypeUnqual(Type::Int8Ty),
694 Context.getPointerTypeUnqual(Type::Int8Ty), (Type *)0);
693695
694696 // Use the function we just added to get addresses of functions we need.
695697 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
700702 // Don't forward functions which are external in the test module too.
701703 if (TestFn && !TestFn->isDeclaration()) {
702704 // 1. Add a string constant with its name to the global file
703 Constant *InitArray = ConstantArray::get(F->getName());
705 Constant *InitArray = Context.getConstantArray(F->getName());
704706 GlobalVariable *funcName =
705707 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
706708 GlobalValue::InternalLinkage, InitArray,
710712 // sbyte* so it matches the signature of the resolver function.
711713
712714 // GetElementPtr *funcName, ulong 0, ulong 0
713 std::vector GEPargs(2,
714 BD.getContext().getNullValue(Type::Int32Ty));
715 Value *GEP = ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
715 std::vector GEPargs(2, Context.getNullValue(Type::Int32Ty));
716 Value *GEP =
717 Context.getConstantExprGetElementPtr(funcName, &GEPargs[0], 2);
716718 std::vector ResolverArgs;
717719 ResolverArgs.push_back(GEP);
718720
720722 // function that dynamically resolves the calls to F via our JIT API
721723 if (!F->use_empty()) {
722724 // Create a new global to hold the cached function pointer.
723 Constant *NullPtr = ConstantPointerNull::get(F->getType());
725 Constant *NullPtr = Context.getConstantPointerNull(F->getType());
724726 GlobalVariable *Cache =
725727 new GlobalVariable(*F->getParent(), F->getType(),
726728 false, GlobalValue::InternalLinkage,
752754 // Cast the result from the resolver to correctly-typed function.
753755 CastInst *CastedResolver =
754756 new BitCastInst(Resolver,
755 PointerType::getUnqual(F->getFunctionType()),
757 Context.getPointerTypeUnqual(F->getFunctionType()),
756758 "resolverCast", LookupBB);
757759
758760 // Save the value in our cache.
2929
3030 Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
3131 std::vector params;
32 const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
32 const FunctionType *FTy =
33 getGlobalContext().getFunctionType(G->getType()->getElementType(),
3334 params, false);
3435 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
3536 BasicBlock *Entry = BasicBlock::Create("entry", F);
3637 IRBuilder<> builder(Entry);
3738 Value *Load = builder.CreateLoad(G);
3839 const Type *GTy = G->getType()->getElementType();
39 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
40 Value *Add = builder.CreateAdd(Load,
41 getGlobalContext().getConstantInt(GTy, 1LL));
4042 builder.CreateStore(Add, G);
4143 builder.CreateRet(Add);
4244 return F;
2424 Constant *ConstantV;
2525 std::auto_ptr BitcastV;
2626
27 ValueHandle() : ConstantV(ConstantInt::get(Type::Int32Ty, 0)),
28 BitcastV(new BitCastInst(ConstantV, Type::Int32Ty)) {
27 ValueHandle() :
28 ConstantV(getGlobalContext().getConstantInt(Type::Int32Ty, 0)),
29 BitcastV(new BitCastInst(ConstantV, Type::Int32Ty)) {
2930 }
3031 };
3132
2222 // MDString objects, even with the same string pointer and nulls in the string.
2323 TEST(MDStringTest, CreateDifferent) {
2424 char x[3] = { 'f', 0, 'A' };
25 MDString *s1 = MDString::get(&x[0], &x[3]);
25 MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]);
2626 x[2] = 'B';
27 MDString *s2 = MDString::get(&x[0], &x[3]);
27 MDString *s2 = getGlobalContext().getMDString(&x[0], &x[3]);
2828 EXPECT_NE(s1, s2);
2929 }
3030
3434 char x[4] = { 'a', 'b', 'c', 'X' };
3535 char y[4] = { 'a', 'b', 'c', 'Y' };
3636
37 MDString *s1 = MDString::get(&x[0], &x[3]);
38 MDString *s2 = MDString::get(&y[0], &y[3]);
37 MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]);
38 MDString *s2 = getGlobalContext().getMDString(&y[0], &y[3]);
3939 EXPECT_EQ(s1, s2);
4040 }
4141
4343 TEST(MDStringTest, PrintingSimple) {
4444 char *str = new char[13];
4545 strncpy(str, "testing 1 2 3", 13);
46 MDString *s = MDString::get(str, str+13);
46 MDString *s = getGlobalContext().getMDString(str, str+13);
4747 strncpy(str, "aaaaaaaaaaaaa", 13);
4848 delete[] str;
4949
5555 // Test printing of MDString with non-printable characters.
5656 TEST(MDStringTest, PrintingComplex) {
5757 char str[5] = {0, '\n', '"', '\\', -1};
58 MDString *s = MDString::get(str+0, str+5);
58 MDString *s = getGlobalContext().getMDString(str+0, str+5);
5959 std::ostringstream oss;
6060 s->print(oss);
6161 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
6666 char x[3] = { 'a', 'b', 'c' };
6767 char y[3] = { '1', '2', '3' };
6868
69 MDString *s1 = MDString::get(&x[0], &x[3]);
70 MDString *s2 = MDString::get(&y[0], &y[3]);
71 ConstantInt *CI = ConstantInt::get(APInt(8, 0));
69 MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]);
70 MDString *s2 = getGlobalContext().getMDString(&y[0], &y[3]);
71 ConstantInt *CI = getGlobalContext().getConstantInt(APInt(8, 0));
7272
7373 std::vector V;
7474 V.push_back(s1);
7575 V.push_back(CI);
7676 V.push_back(s2);
7777
78 MDNode *n1 = MDNode::get(&V[0], 3);
78 MDNode *n1 = getGlobalContext().getMDNode(&V[0], 3);
7979 Value *const c1 = n1;
80 MDNode *n2 = MDNode::get(&c1, 1);
81 MDNode *n3 = MDNode::get(&V[0], 3);
80 MDNode *n2 = getGlobalContext().getMDNode(&c1, 1);
81 MDNode *n3 = getGlobalContext().getMDNode(&V[0], 3);
8282 EXPECT_NE(n1, n2);
8383 EXPECT_EQ(n1, n3);
8484
101101 }
102102
103103 TEST(MDNodeTest, RAUW) {
104 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
104 Constant *C = getGlobalContext().getConstantInt(Type::Int32Ty, 1);
105105 Instruction *I = new BitCastInst(C, Type::Int32Ty);
106106
107107 Value *const V1 = I;
108 MDNode *n1 = MDNode::get(&V1, 1);
108 MDNode *n1 = getGlobalContext().getMDNode(&V1, 1);
109109 WeakVH wn1 = n1;
110110
111111 Value *const V2 = C;
112 MDNode *n2 = MDNode::get(&V2, 1);
112 MDNode *n2 = getGlobalContext().getMDNode(&V2, 1);
113113 WeakVH wn2 = n2;
114114
115115 EXPECT_NE(wn1, wn2);
120120 }
121121
122122 TEST(MDNodeTest, Delete) {
123 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
123 Constant *C = getGlobalContext().getConstantInt(Type::Int32Ty, 1);
124124 Instruction *I = new BitCastInst(C, Type::Int32Ty);
125125
126126 Value *const V = I;
127 MDNode *n = MDNode::get(&V, 1);
127 MDNode *n = getGlobalContext().getMDNode(&V, 1);
128128 WeakVH wvh = n;
129129
130130 EXPECT_EQ(n, wvh);