llvm.org GIT mirror llvm / db8d2be
For PR950: Convert signed integer types to signless. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32786 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 12 years ago
6 changed file(s) with 65 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
3737 static Function *CreateFibFunction(Module *M) {
3838 // Create the fib function and insert it into module M. This function is said
3939 // to return an int and take an int parameter.
40 Function *FibF = M->getOrInsertFunction("fib", Type::IntTy, Type::IntTy,
40 Function *FibF = M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
4141 (Type *)0);
4242
4343 // Add a basic block to the function.
4444 BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
4545
4646 // Get pointers to the constants.
47 Value *One = ConstantInt::get(Type::IntTy, 1);
48 Value *Two = ConstantInt::get(Type::IntTy, 2);
47 Value *One = ConstantInt::get(Type::Int32Ty, 1);
48 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
4949
5050 // Get pointer to the integer argument of the add1 function...
5151 Argument *ArgX = FibF->arg_begin(); // Get the arg.
110110
111111 // Call the Fibonacci function with argument n:
112112 std::vector Args(1);
113 Args[0].IntVal = n;
113 Args[0].Int32Val = n;
114114 GenericValue GV = EE->runFunction(FibF, Args);
115115
116116 // import result of execution
117 std::cout << "Result: " << GV.IntVal << "\n";
117 std::cout << "Result: " << GV.Int32Val << "\n";
118118 return 0;
119119 }
5151 // Create the add1 function entry and insert this entry into module M. The
5252 // function will have a return type of "int" and take an argument of "int".
5353 // The '0' terminates the list of argument types.
54 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy,
54 Function *Add1F = M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
5555 (Type *)0);
5656
5757 // Add a basic block to the function. As before, it automatically inserts
5959 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
6060
6161 // Get pointers to the constant `1'.
62 Value *One = ConstantInt::get(Type::IntTy, 1);
62 Value *One = ConstantInt::get(Type::Int32Ty, 1);
6363
6464 // Get pointers to the integer argument of the add1 function...
6565 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
7777
7878 // Now we going to create function `foo', which returns an int and takes no
7979 // arguments.
80 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, (Type *)0);
80 Function *FooF = M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0);
8181
8282 // Add a basic block to the FooF function.
8383 BB = new BasicBlock("EntryBlock", FooF);
8484
8585 // Get pointers to the constant `10'.
86 Value *Ten = ConstantInt::get(Type::IntTy, 10);
86 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
8787
8888 // Pass Ten to the call call:
8989 std::vector Params;
106106 GenericValue gv = EE->runFunction(FooF, noargs);
107107
108108 // Import result of execution:
109 std::cout << "Result: " << gv.IntVal << "\n";
109 std::cout << "Result: " << gv.Int32Val << "\n";
110110 return 0;
111111 }
2626 Module *M = new Module("test");
2727
2828 // Create the main function: first create the type 'int ()'
29 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector(),
29 FunctionType *FT = FunctionType::get(Type::Int32Ty, std::vector(),
3030 /*not vararg*/false);
3131
3232 // By passing a module as the last parameter to the Function constructor,
3838 BasicBlock *BB = new BasicBlock("EntryBlock", F);
3939
4040 // Get pointers to the constant integers...
41 Value *Two = ConstantInt::get(Type::IntTy, 2);
42 Value *Three = ConstantInt::get(Type::IntTy, 3);
41 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
42 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
4343
4444 // Create the add instruction... does not insert...
4545 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
3333 // Create the add1 function entry and insert this entry into module M. The
3434 // function will have a return type of "int" and take an argument of "int".
3535 // The '0' terminates the list of argument types.
36 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy,
36 Function *Add1F = M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
3737 (Type *)0);
3838
3939 // Add a basic block to the function. As before, it automatically inserts
4141 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
4242
4343 // Get pointers to the constant `1'.
44 Value *One = ConstantInt::get(Type::IntTy, 1);
44 Value *One = ConstantInt::get(Type::Int32Ty, 1);
4545
4646 // Get pointers to the integer argument of the add1 function...
4747 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
6262 {
6363 // Create the fib function and insert it into module M. This function is said
6464 // to return an int and take an int parameter.
65 Function *FibF = M->getOrInsertFunction("fib", Type::IntTy, Type::IntTy,
65 Function *FibF = M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
6666 (Type *)0);
6767
6868 // Add a basic block to the function.
6969 BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
7070
7171 // Get pointers to the constants.
72 Value *One = ConstantInt::get(Type::IntTy, 1);
73 Value *Two = ConstantInt::get(Type::IntTy, 2);
72 Value *One = ConstantInt::get(Type::Int32Ty, 1);
73 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
7474
7575 // Get pointer to the integer argument of the add1 function...
7676 Argument *ArgX = FibF->arg_begin(); // Get the arg.
220220
221221 // Call the `foo' function with no arguments:
222222 std::vector Args(1);
223 Args[0].IntVal = p->value;
223 Args[0].Int32Val = p->value;
224224
225225 synchronize.block(); // wait until other threads are at this point
226226 GenericValue gv = p->EE->runFunction(p->F, Args);
227227
228 return (void*) intptr_t(gv.IntVal);
228 return (void*) intptr_t(gv.Int32Val);
229229 }
230230
231231 int main()
121121
122122 // Create a type to represent the stack. This is the same as the LLVM
123123 // Assembly type [ 256 x long ]
124 stack_type = ArrayType::get( Type::LongTy, stack_size );
124 stack_type = ArrayType::get( Type::Int64Ty, stack_size );
125125
126126 // Create a global variable for the stack. Note the use of appending
127127 // linkage linkage so that multiple modules will make the stack larger.
140140 // of LinkOnce linkage. Only one copy of _index_ will be retained
141141 // after linking
142142 TheIndex = new GlobalVariable(
143 /*type=*/Type::LongTy,
143 /*type=*/Type::Int64Ty,
144144 /*isConstant=*/false,
145145 /*Linkage=*/GlobalValue::LinkOnceLinkage,
146 /*initializer=*/ Constant::getNullValue(Type::LongTy),
146 /*initializer=*/ Constant::getNullValue(Type::Int64Ty),
147147 /*name=*/"_index_",
148148 /*parent=*/TheModule
149149 );
154154 DefinitionType = FunctionType::get( Type::VoidTy, params, false );
155155
156156 // Create a function for printf(3)
157 params.push_back( PointerType::get( Type::SByteTy ) );
157 params.push_back( PointerType::get( Type::Int8Ty ) );
158158 FunctionType* printf_type =
159 FunctionType::get( Type::IntTy, params, true );
159 FunctionType::get( Type::Int32Ty, params, true );
160160 ThePrintf = new Function(
161161 printf_type, GlobalValue::ExternalLinkage, "printf", TheModule);
162162
166166
167167 // Create a function for exit(3)
168168 params.clear();
169 params.push_back( Type::IntTy );
169 params.push_back( Type::Int32Ty );
170170 FunctionType* exit_type =
171171 FunctionType::get( Type::VoidTy, params, false );
172172 TheExit = new Function(
174174
175175 Constant* str_format = ConstantArray::get("%s");
176176 StrFormat = new GlobalVariable(
177 /*type=*/ArrayType::get( Type::SByteTy, 3 ),
177 /*type=*/ArrayType::get( Type::Int8Ty, 3 ),
178178 /*isConstant=*/true,
179179 /*Linkage=*/GlobalValue::LinkOnceLinkage,
180180 /*initializer=*/str_format,
184184
185185 Constant* in_str_format = ConstantArray::get(" %as");
186186 InStrFormat = new GlobalVariable(
187 /*type=*/ArrayType::get( Type::SByteTy, 5 ),
187 /*type=*/ArrayType::get( Type::Int8Ty, 5 ),
188188 /*isConstant=*/true,
189189 /*Linkage=*/GlobalValue::LinkOnceLinkage,
190190 /*initializer=*/in_str_format,
194194
195195 Constant* num_format = ConstantArray::get("%d");
196196 NumFormat = new GlobalVariable(
197 /*type=*/ArrayType::get( Type::SByteTy, 3 ),
197 /*type=*/ArrayType::get( Type::Int8Ty, 3 ),
198198 /*isConstant=*/true,
199199 /*Linkage=*/GlobalValue::LinkOnceLinkage,
200200 /*initializer=*/num_format,
204204
205205 Constant* in_num_format = ConstantArray::get(" %d");
206206 InNumFormat = new GlobalVariable(
207 /*type=*/ArrayType::get( Type::SByteTy, 4 ),
207 /*type=*/ArrayType::get( Type::Int8Ty, 4 ),
208208 /*isConstant=*/true,
209209 /*Linkage=*/GlobalValue::LinkOnceLinkage,
210210 /*initializer=*/in_num_format,
214214
215215 Constant* chr_format = ConstantArray::get("%c");
216216 ChrFormat = new GlobalVariable(
217 /*type=*/ArrayType::get( Type::SByteTy, 3 ),
217 /*type=*/ArrayType::get( Type::Int8Ty, 3 ),
218218 /*isConstant=*/true,
219219 /*Linkage=*/GlobalValue::LinkOnceLinkage,
220220 /*initializer=*/chr_format,
224224
225225 Constant* in_chr_format = ConstantArray::get(" %c");
226226 InChrFormat = new GlobalVariable(
227 /*type=*/ArrayType::get( Type::SByteTy, 4 ),
227 /*type=*/ArrayType::get( Type::Int8Ty, 4 ),
228228 /*isConstant=*/true,
229229 /*Linkage=*/GlobalValue::LinkOnceLinkage,
230230 /*initializer=*/in_chr_format,
233233 );
234234
235235 // Get some constants so we aren't always creating them
236 Zero = ConstantInt::get( Type::LongTy, 0 );
237 One = ConstantInt::get( Type::LongTy, 1 );
238 Two = ConstantInt::get( Type::LongTy, 2 );
239 Three = ConstantInt::get( Type::LongTy, 3 );
240 Four = ConstantInt::get( Type::LongTy, 4 );
241 Five = ConstantInt::get( Type::LongTy, 5 );
236 Zero = ConstantInt::get( Type::Int64Ty, 0 );
237 One = ConstantInt::get( Type::Int64Ty, 1 );
238 Two = ConstantInt::get( Type::Int64Ty, 2 );
239 Three = ConstantInt::get( Type::Int64Ty, 3 );
240 Four = ConstantInt::get( Type::Int64Ty, 4 );
241 Five = ConstantInt::get( Type::Int64Ty, 5 );
242242
243243 // Reset the current line number
244244 Stackerlineno = 1;
362362
363363 // Increment the loaded index value
364364 if ( ival == 0 ) ival = One;
365 CastInst* caster = CastInst::createSExtOrBitCast( ival, Type::LongTy );
365 CastInst* caster = CastInst::createSExtOrBitCast( ival, Type::Int64Ty );
366366 bb->getInstList().push_back( caster );
367367 BinaryOperator* addop = BinaryOperator::create( Instruction::Add,
368368 loadop, caster);
383383
384384 // Decrement the loaded index value
385385 if ( ival == 0 ) ival = One;
386 CastInst* caster = CastInst::createSExtOrBitCast( ival, Type::LongTy );
386 CastInst* caster = CastInst::createSExtOrBitCast( ival, Type::Int64Ty );
387387 bb->getInstList().push_back( caster );
388388 BinaryOperator* subop = BinaryOperator::create( Instruction::Sub,
389389 loadop, caster);
417417 }
418418 else
419419 {
420 CastInst* caster = CastInst::createSExtOrBitCast( index, Type::LongTy );
420 CastInst* caster = CastInst::createSExtOrBitCast( index, Type::Int64Ty );
421421 bb->getInstList().push_back( caster );
422422 BinaryOperator* subop = BinaryOperator::create(
423423 Instruction::Sub, loadop, caster );
447447 (isa(val->getType()) ? Instruction::PtrToInt :
448448 (val->getType()->getPrimitiveSizeInBits() < 64 ? Instruction::SExt :
449449 Instruction::BitCast));
450 CastInst* cast_inst = CastInst::create(opcode, val, Type::LongTy );
450 CastInst* cast_inst = CastInst::create(opcode, val, Type::Int64Ty );
451451 bb->getInstList().push_back( cast_inst );
452452
453453 // Store the value
461461 StackerCompiler::push_integer(BasicBlock* bb, int64_t value )
462462 {
463463 // Just push a constant integer value
464 return push_value( bb, ConstantInt::get( Type::LongTy, value ) );
464 return push_value( bb, ConstantInt::get( Type::Int64Ty, value ) );
465465 }
466466
467467 Instruction*
490490
491491 // Create a type for the string constant. Length is +1 for
492492 // the terminating 0.
493 ArrayType* char_array = ArrayType::get( Type::SByteTy, len + 1 );
493 ArrayType* char_array = ArrayType::get( Type::Int8Ty, len + 1 );
494494
495495 // Create an initializer for the value
496496 Constant* initVal = ConstantArray::get( value );
522522
523523 // Cast the integer to a sbyte*
524524 CastInst* caster =
525 new IntToPtrInst(loader, PointerType::get(Type::SByteTy));
525 new IntToPtrInst(loader, PointerType::get(Type::Int8Ty));
526526 bb->getInstList().push_back( caster );
527527
528528 // Decrement stack index
575575
576576 // Cast the integer to a sbyte*
577577 CastInst* caster =
578 new IntToPtrInst(loader, PointerType::get(Type::SByteTy) );
578 new IntToPtrInst(loader, PointerType::get(Type::Int8Ty) );
579579 bb->getInstList().push_back( caster );
580580
581581 // Return the value
651651 TheStack->setLinkage( GlobalValue::LinkOnceLinkage );
652652
653653 // Turn "_index_" into an intialized variable for the same reason.
654 TheIndex->setInitializer( Constant::getNullValue(Type::LongTy) );
654 TheIndex->setInitializer( Constant::getNullValue(Type::Int64Ty) );
655655 TheIndex->setLinkage( GlobalValue::LinkOnceLinkage );
656656
657657 return func;
721721
722722 // Compare the condition against 0
723723 ICmpInst* cond_inst = new ICmpInst( ICmpInst::ICMP_NE, cond,
724 ConstantInt::get( Type::LongTy, 0) );
724 ConstantInt::get( Type::Int64Ty, 0) );
725725 bb->getInstList().push_back( cond_inst );
726726
727727 // Create an exit block
805805
806806 // Compare the condition against 0
807807 ICmpInst* cond_inst = new ICmpInst(
808 ICmpInst::ICMP_NE, cond, ConstantInt::get( Type::LongTy, 0));
808 ICmpInst::ICMP_NE, cond, ConstantInt::get( Type::Int64Ty, 0));
809809 test->getInstList().push_back( cond_inst );
810810
811811 // Add the branch instruction
10191019 if (echo) bb->setName("DECR");
10201020 LoadInst* op1 = cast(pop_integer(bb));
10211021 BinaryOperator* subop = BinaryOperator::create( Instruction::Sub, op1,
1022 ConstantInt::get( Type::LongTy, 1 ) );
1022 ConstantInt::get( Type::Int64Ty, 1 ) );
10231023 bb->getInstList().push_back( subop );
10241024 push_value( bb, subop );
10251025 break;
10891089 // bb->getInstList().push_back( negop );
10901090 // So we'll multiply by -1 (ugh)
10911091 BinaryOperator* multop = BinaryOperator::create( Instruction::Mul, op1,
1092 ConstantInt::get( Type::LongTy, -1 ) );
1092 ConstantInt::get( Type::Int64Ty, -1 ) );
10931093 bb->getInstList().push_back( multop );
10941094 push_value( bb, multop );
10951095 break;
12441244 if (echo) bb->setName("SHL");
12451245 LoadInst* op1 = cast(pop_integer(bb));
12461246 LoadInst* op2 = cast(pop_integer(bb));
1247 CastInst* castop = new TruncInst( op1, Type::UByteTy );
1247 CastInst* castop = new TruncInst( op1, Type::Int8Ty );
12481248 bb->getInstList().push_back( castop );
12491249 ShiftInst* shlop = new ShiftInst( Instruction::Shl, op2, castop );
12501250 bb->getInstList().push_back( shlop );
12561256 if (echo) bb->setName("SHR");
12571257 LoadInst* op1 = cast(pop_integer(bb));
12581258 LoadInst* op2 = cast(pop_integer(bb));
1259 CastInst* castop = new TruncInst( op1, Type::UByteTy );
1259 CastInst* castop = new TruncInst( op1, Type::Int8Ty );
12601260 bb->getInstList().push_back( castop );
12611261 ShiftInst* shrop = new ShiftInst( Instruction::AShr, op2, castop );
12621262 bb->getInstList().push_back( shrop );
14781478 LoadInst* op1 = cast( pop_integer(bb) );
14791479
14801480 // Make sure its a UIntTy
1481 CastInst* caster = CastInst::createTruncOrBitCast( op1, Type::UIntTy );
1481 CastInst* caster = CastInst::createTruncOrBitCast( op1, Type::Int32Ty );
14821482 bb->getInstList().push_back( caster );
14831483
14841484 // Allocate the bytes
1485 MallocInst* mi = new MallocInst( Type::SByteTy, caster );
1485 MallocInst* mi = new MallocInst( Type::Int8Ty, caster );
14861486 bb->getInstList().push_back( mi );
14871487
14881488 // Push the pointer
15061506 if (echo) bb->setName("GET");
15071507 // Get the character index
15081508 LoadInst* op1 = cast( stack_top(bb) );
1509 CastInst* chr_idx = CastInst::createSExtOrBitCast( op1, Type::LongTy );
1509 CastInst* chr_idx = CastInst::createSExtOrBitCast( op1, Type::Int64Ty );
15101510 bb->getInstList().push_back( chr_idx );
15111511
15121512 // Get the String pointer
15211521 // Get the value and push it
15221522 LoadInst* loader = new LoadInst( gep );
15231523 bb->getInstList().push_back( loader );
1524 CastInst* caster = CastInst::createTruncOrBitCast(loader, Type::IntTy);
1524 CastInst* caster = CastInst::createTruncOrBitCast(loader, Type::Int32Ty);
15251525 bb->getInstList().push_back( caster );
15261526
15271527 // Push the result back on stack
15381538
15391539 // Get the character index
15401540 LoadInst* w2 = cast( pop_integer(bb) );
1541 CastInst* chr_idx = CastInst::createSExtOrBitCast( w2, Type::LongTy );
1541 CastInst* chr_idx = CastInst::createSExtOrBitCast( w2, Type::Int64Ty );
15421542 bb->getInstList().push_back( chr_idx );
15431543
15441544 // Get the String pointer
15511551 bb->getInstList().push_back( gep );
15521552
15531553 // Cast the value and put it
1554 CastInst* caster = new TruncInst( w1, Type::SByteTy );
1554 CastInst* caster = new TruncInst( w1, Type::Int8Ty );
15551555 bb->getInstList().push_back( caster );
15561556 StoreInst* storer = new StoreInst( caster, gep );
15571557 bb->getInstList().push_back( storer );
15791579 LoadInst* op1 = cast(pop_integer(bb));
15801580
15811581 // Cast down to an integer
1582 CastInst* caster = new TruncInst( op1, Type::IntTy );
1582 CastInst* caster = new TruncInst( op1, Type::Int32Ty );
15831583 bb->getInstList().push_back( caster );
15841584
15851585 // Call exit(3)
16011601 bb->getInstList().push_back( format_gep );
16021602
16031603 // Get the character to print (a tab)
1604 ConstantInt* newline = ConstantInt::get(Type::IntTy,
1604 ConstantInt* newline = ConstantInt::get(Type::Int32Ty,
16051605 static_cast('\t'));
16061606
16071607 // Call printf
16231623 bb->getInstList().push_back( format_gep );
16241624
16251625 // Get the character to print (a space)
1626 ConstantInt* newline = ConstantInt::get(Type::IntTy,
1626 ConstantInt* newline = ConstantInt::get(Type::Int32Ty,
16271627 static_cast(' '));
16281628
16291629 // Call printf
16451645 bb->getInstList().push_back( format_gep );
16461646
16471647 // Get the character to print (a newline)
1648 ConstantInt* newline = ConstantInt::get(Type::IntTy,
1648 ConstantInt* newline = ConstantInt::get(Type::Int32Ty,
16491649 static_cast('\n'));
16501650
16511651 // Call printf
16631663 GetElementPtrInst* gep_value =
16641664 cast(get_stack_pointer(bb));
16651665 CastInst* caster =
1666 new BitCastInst(gep_value, PointerType::get(Type::SByteTy));
1666 new BitCastInst(gep_value, PointerType::get(Type::Int8Ty));
16671667
16681668 // Make room for the count result
16691669 incr_stack_index(bb);
3030 std::string TargetPrefix; // Target prefix, e.g. "ppc" for t-s intrinsics.
3131
3232 /// ArgTypes - The type primitive enum value for the return value and all
33 /// of the arguments. These are things like Type::UIntTyID.
33 /// of the arguments. These are things like Type::Int32TyID.
3434 std::vector ArgTypes;
3535
3636 /// ArgVTs - The MVT::ValueType for each argument type. Note that this list