blob: 0eac79fbea8394fbcad779b158fc19f90eac48f3 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Bill Wendlingfae14752011-08-12 20:24:12 +0000157//===----------------------------------------------------------------------===//
158// LandingPadInst Implementation
159//===----------------------------------------------------------------------===//
160
David Majnemer7fddecc2015-06-17 20:52:32 +0000161LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
162 const Twine &NameStr, Instruction *InsertBefore)
163 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
164 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000165}
166
David Majnemer7fddecc2015-06-17 20:52:32 +0000167LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
168 const Twine &NameStr, BasicBlock *InsertAtEnd)
169 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
170 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000171}
172
173LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000174 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
175 LP.getNumOperands()),
176 ReservedSpace(LP.getNumOperands()) {
177 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000178 Use *OL = getOperandList();
179 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000180 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
181 OL[I] = InOL[I];
182
183 setCleanup(LP.isCleanup());
184}
185
David Majnemer7fddecc2015-06-17 20:52:32 +0000186LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000187 const Twine &NameStr,
188 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000189 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000190}
191
David Majnemer7fddecc2015-06-17 20:52:32 +0000192LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000193 const Twine &NameStr,
194 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000195 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000196}
197
David Majnemer7fddecc2015-06-17 20:52:32 +0000198void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000199 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000200 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000201 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000202 setName(NameStr);
203 setCleanup(false);
204}
205
206/// growOperands - grow operands - This grows the operand list in response to a
207/// push_back style of operation. This grows the number of ops by 2 times.
208void LandingPadInst::growOperands(unsigned Size) {
209 unsigned e = getNumOperands();
210 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000211 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000212 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000213}
214
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000215void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000216 unsigned OpNo = getNumOperands();
217 growOperands(1);
218 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000219 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000220 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000221}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222
223//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224// CallInst Implementation
225//===----------------------------------------------------------------------===//
226
Gordon Henriksen14a55692007-12-10 02:14:30 +0000227CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
David Blaikie348de692015-04-23 21:36:23 +0000230void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000231 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000232 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000233 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
234 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000235 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236
Jay Foad5bd375a2011-07-15 08:37:34 +0000237#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000238 assert((Args.size() == FTy->getNumParams() ||
239 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000240 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000241
242 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000243 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000244 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000245 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000246#endif
247
248 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000249
250 auto It = populateBundleOperandInfos(Bundles, Args.size());
251 (void)It;
252 assert(It + 1 == op_end() && "Should add up!");
253
Jay Foad5bd375a2011-07-15 08:37:34 +0000254 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Jay Foad5bd375a2011-07-15 08:37:34 +0000257void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000258 FTy =
259 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000260 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000261 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264
265 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Daniel Dunbar4975db62009-07-25 04:41:11 +0000268CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 Instruction *InsertBefore)
270 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
271 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000272 Instruction::Call,
273 OperandTraits<CallInst>::op_end(this) - 1,
274 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000275 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276}
277
Daniel Dunbar4975db62009-07-25 04:41:11 +0000278CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 BasicBlock *InsertAtEnd)
280 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
281 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 Instruction::Call,
283 OperandTraits<CallInst>::op_end(this) - 1,
284 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000285 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000289 : Instruction(CI.getType(), Instruction::Call,
290 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
291 CI.getNumOperands()),
292 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000293 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000294 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000295
Jay Foad5bd375a2011-07-15 08:37:34 +0000296 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000297 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
298 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000299 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Sanjoy Das2d161452015-11-18 06:23:38 +0000302CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
303 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000304 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000305
306 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
307 InsertPt);
308 NewCI->setTailCallKind(CI->getTailCallKind());
309 NewCI->setCallingConv(CI->getCallingConv());
310 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000311 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000312 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000313 return NewCI;
314}
315
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000316void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000317 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000318 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000319 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000320}
321
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000322void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
323 AttributeSet PAL = getAttributes();
324 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
325 setAttributes(PAL);
326}
327
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000328void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000329 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000330 AttrBuilder B(attr);
331 LLVMContext &Context = getContext();
332 PAL = PAL.removeAttributes(Context, i,
333 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000334 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000335}
336
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000337void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
338 AttributeSet PAL = getAttributes();
339 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
340 setAttributes(PAL);
341}
342
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000343void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
344 AttributeSet PAL = getAttributes();
345 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
346 setAttributes(PAL);
347}
348
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000349bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000350 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
351
Bill Wendling749a43d2012-12-30 13:50:49 +0000352 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000353 return true;
354 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000355 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000356 return false;
357}
358
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000359bool CallInst::dataOperandHasImpliedAttr(unsigned i,
360 Attribute::AttrKind A) const {
361
Sanjoy Das776e4a72015-11-05 01:53:26 +0000362 // There are getNumOperands() - 1 data operands. The last operand is the
363 // callee.
364 assert(i < getNumOperands() && "Data operand index out of bounds!");
365
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000366 // The attribute A can either be directly specified, if the operand in
367 // question is a call argument; or be indirectly implied by the kind of its
368 // containing operand bundle, if the operand is a bundle operand.
369
370 if (i < (getNumArgOperands() + 1))
371 return paramHasAttr(i, A);
372
373 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
374 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000375 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000376}
377
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378/// IsConstantOne - Return true only if val is constant int 1
379static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000380 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000381 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
382 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000383}
384
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000385static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000386 BasicBlock *InsertAtEnd, Type *IntPtrTy,
387 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 Value *ArraySize, Function *MallocF,
389 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000390 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000391 "createMalloc needs either InsertBefore or InsertAtEnd");
392
393 // malloc(type) becomes:
394 // bitcast (i8* malloc(typeSize)) to type*
395 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000396 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000397 if (!ArraySize)
398 ArraySize = ConstantInt::get(IntPtrTy, 1);
399 else if (ArraySize->getType() != IntPtrTy) {
400 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000401 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
402 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000403 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000404 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
405 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000406 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000408 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000409 if (IsConstantOne(AllocSize)) {
410 AllocSize = ArraySize; // Operand * 1 = Operand
411 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
412 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
413 false /*ZExt*/);
414 // Malloc arg is constant product of type size and array size
415 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
416 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000417 // Multiply type size by the array size...
418 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000419 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
420 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000421 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000422 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
423 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000424 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000425 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000426
Victor Hernandez788eaab2009-09-18 19:20:02 +0000427 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000428 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000429 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
430 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000431 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000432 Value *MallocFunc = MallocF;
433 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000434 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000435 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000436 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000437 CallInst *MCall = nullptr;
438 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000439 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000440 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000441 Result = MCall;
442 if (Result->getType() != AllocPtrType)
443 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000444 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000445 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000446 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000447 Result = MCall;
448 if (Result->getType() != AllocPtrType) {
449 InsertAtEnd->getInstList().push_back(MCall);
450 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000451 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000452 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000453 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000454 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000455 if (Function *F = dyn_cast<Function>(MallocFunc)) {
456 MCall->setCallingConv(F->getCallingConv());
457 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
458 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000459 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000460
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000461 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000462}
463
464/// CreateMalloc - Generate the IR for a call to malloc:
465/// 1. Compute the malloc call's argument as the specified type's size,
466/// possibly multiplied by the array size if the array size is not
467/// constant 1.
468/// 2. Call malloc with that argument.
469/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000470Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000471 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000472 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000473 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000474 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000475 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000476 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477}
478
479/// CreateMalloc - Generate the IR for a call to malloc:
480/// 1. Compute the malloc call's argument as the specified type's size,
481/// possibly multiplied by the array size if the array size is not
482/// constant 1.
483/// 2. Call malloc with that argument.
484/// 3. Bitcast the result of the malloc call to the specified type.
485/// Note: This function does not add the bitcast to the basic block, that is the
486/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000487Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000488 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000489 Value *AllocSize, Value *ArraySize,
490 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000491 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000492 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000494
Ana Pazosb3596022016-02-03 21:34:39 +0000495static Instruction *createFree(Value *Source, Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000496 BasicBlock *InsertAtEnd) {
497 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
498 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000499 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000500 "Can not free something of nonpointer type!");
501
Ana Pazosb3596022016-02-03 21:34:39 +0000502 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
503 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000504
Chris Lattner229907c2011-07-18 04:54:35 +0000505 Type *VoidTy = Type::getVoidTy(M->getContext());
506 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000507 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000508 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Ana Pazosb3596022016-02-03 21:34:39 +0000509 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000510 Value *PtrCast = Source;
511 if (InsertBefore) {
512 if (Source->getType() != IntPtrTy)
513 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
514 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
515 } else {
516 if (Source->getType() != IntPtrTy)
517 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
518 Result = CallInst::Create(FreeFunc, PtrCast, "");
519 }
520 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000521 if (Function *F = dyn_cast<Function>(FreeFunc))
522 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000523
524 return Result;
525}
526
527/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000528Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000529 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000530}
531
532/// CreateFree - Generate the IR for a call to the builtin free function.
533/// Note: This function does not add the call to the basic block, that is the
534/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000535Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
536 Instruction *FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000537 assert(FreeCall && "CreateFree did not create a CallInst");
538 return FreeCall;
539}
540
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541//===----------------------------------------------------------------------===//
542// InvokeInst Implementation
543//===----------------------------------------------------------------------===//
544
David Blaikie3e807092015-05-13 18:35:26 +0000545void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
546 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000547 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000548 const Twine &NameStr) {
549 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000550
Sanjoy Das9303c242015-09-24 19:14:18 +0000551 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
552 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000553 Op<-3>() = Fn;
554 Op<-2>() = IfNormal;
555 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000556
557#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000558 assert(((Args.size() == FTy->getNumParams()) ||
559 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000560 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000561
Jay Foad5bd375a2011-07-15 08:37:34 +0000562 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000563 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000564 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000565 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000566#endif
567
568 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000569
570 auto It = populateBundleOperandInfos(Bundles, Args.size());
571 (void)It;
572 assert(It + 3 == op_end() && "Should add up!");
573
Jay Foad5bd375a2011-07-15 08:37:34 +0000574 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575}
576
Misha Brukmanb1c93172005-04-21 23:48:37 +0000577InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000578 : TerminatorInst(II.getType(), Instruction::Invoke,
579 OperandTraits<InvokeInst>::op_end(this) -
580 II.getNumOperands(),
581 II.getNumOperands()),
582 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000583 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000584 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000585 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
586 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000587 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000588}
589
Sanjoy Das2d161452015-11-18 06:23:38 +0000590InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
591 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000592 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000593
594 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
595 II->getUnwindDest(), Args, OpB,
596 II->getName(), InsertPt);
597 NewII->setCallingConv(II->getCallingConv());
598 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000599 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000600 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000601 return NewII;
602}
603
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
605 return getSuccessor(idx);
606}
607unsigned InvokeInst::getNumSuccessorsV() const {
608 return getNumSuccessors();
609}
610void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
611 return setSuccessor(idx, B);
612}
613
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000614bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000615 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
616
Bill Wendling749a43d2012-12-30 13:50:49 +0000617 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000618 return true;
619 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000620 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000621 return false;
622}
623
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000624bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
625 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000626 // There are getNumOperands() - 3 data operands. The last three operands are
627 // the callee and the two successor basic blocks.
628 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
629
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000630 // The attribute A can either be directly specified, if the operand in
631 // question is an invoke argument; or be indirectly implied by the kind of its
632 // containing operand bundle, if the operand is a bundle operand.
633
634 if (i < (getNumArgOperands() + 1))
635 return paramHasAttr(i, A);
636
637 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
638 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000639 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000640}
641
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000642void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000643 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000644 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000645 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000646}
647
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000648void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000649 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000650 AttrBuilder B(attr);
651 PAL = PAL.removeAttributes(getContext(), i,
652 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000653 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000654}
655
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000656void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
657 AttributeSet PAL = getAttributes();
658 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
659 setAttributes(PAL);
660}
661
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000662void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
663 AttributeSet PAL = getAttributes();
664 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
665 setAttributes(PAL);
666}
667
Bill Wendlingfae14752011-08-12 20:24:12 +0000668LandingPadInst *InvokeInst::getLandingPadInst() const {
669 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
670}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000671
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672//===----------------------------------------------------------------------===//
673// ReturnInst Implementation
674//===----------------------------------------------------------------------===//
675
Chris Lattner2195fc42007-02-24 00:55:48 +0000676ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000677 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000678 OperandTraits<ReturnInst>::op_end(this) -
679 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000680 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000681 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000682 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000683 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000684}
685
Owen Anderson55f1c092009-08-13 21:58:54 +0000686ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
687 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000688 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
689 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000690 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000691 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000692}
Owen Anderson55f1c092009-08-13 21:58:54 +0000693ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
694 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000695 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
696 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000697 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000699}
Owen Anderson55f1c092009-08-13 21:58:54 +0000700ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000702 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000703}
704
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705unsigned ReturnInst::getNumSuccessorsV() const {
706 return getNumSuccessors();
707}
708
Devang Patelae682fb2008-02-26 17:56:20 +0000709/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
710/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000712 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713}
714
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000716 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717}
718
Devang Patel59643e52008-02-23 00:35:18 +0000719ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000720}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000723// ResumeInst Implementation
724//===----------------------------------------------------------------------===//
725
726ResumeInst::ResumeInst(const ResumeInst &RI)
727 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
728 OperandTraits<ResumeInst>::op_begin(this), 1) {
729 Op<0>() = RI.Op<0>();
730}
731
732ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
733 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
734 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
735 Op<0>() = Exn;
736}
737
738ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
739 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
740 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
741 Op<0>() = Exn;
742}
743
744unsigned ResumeInst::getNumSuccessorsV() const {
745 return getNumSuccessors();
746}
747
748void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
749 llvm_unreachable("ResumeInst has no successors!");
750}
751
752BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
753 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000754}
755
756//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000757// CleanupReturnInst Implementation
758//===----------------------------------------------------------------------===//
759
760CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
761 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
762 OperandTraits<CleanupReturnInst>::op_end(this) -
763 CRI.getNumOperands(),
764 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000765 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000766 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000767 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000768 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000769}
770
David Majnemer8a1c45d2015-12-12 05:38:55 +0000771void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000772 if (UnwindBB)
773 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000774
David Majnemer8a1c45d2015-12-12 05:38:55 +0000775 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000776 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000777 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000778}
779
David Majnemer8a1c45d2015-12-12 05:38:55 +0000780CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
781 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000782 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
783 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000784 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
785 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000786 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000787}
788
David Majnemer8a1c45d2015-12-12 05:38:55 +0000789CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
790 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000791 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
792 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000793 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
794 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000795 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000796}
797
798BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
799 assert(Idx == 0);
800 return getUnwindDest();
801}
802unsigned CleanupReturnInst::getNumSuccessorsV() const {
803 return getNumSuccessors();
804}
805void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
806 assert(Idx == 0);
807 setUnwindDest(B);
808}
809
810//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000811// CatchReturnInst Implementation
812//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000813void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000814 Op<0>() = CatchPad;
815 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000816}
David Majnemer654e1302015-07-31 17:58:14 +0000817
818CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
819 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000820 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
821 Op<0>() = CRI.Op<0>();
822 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000823}
824
David Majnemer8a1c45d2015-12-12 05:38:55 +0000825CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000826 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000827 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000828 OperandTraits<CatchReturnInst>::op_begin(this), 2,
829 InsertBefore) {
830 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000831}
832
David Majnemer8a1c45d2015-12-12 05:38:55 +0000833CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000834 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000835 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000836 OperandTraits<CatchReturnInst>::op_begin(this), 2,
837 InsertAtEnd) {
838 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000839}
840
841BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000842 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000843 return getSuccessor();
844}
845unsigned CatchReturnInst::getNumSuccessorsV() const {
846 return getNumSuccessors();
847}
848void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000849 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000850 setSuccessor(B);
851}
852
853//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000854// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000855//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000856
857CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
858 unsigned NumReservedValues,
859 const Twine &NameStr,
860 Instruction *InsertBefore)
861 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
862 InsertBefore) {
863 if (UnwindDest)
864 ++NumReservedValues;
865 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000866 setName(NameStr);
867}
868
David Majnemer8a1c45d2015-12-12 05:38:55 +0000869CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
870 unsigned NumReservedValues,
871 const Twine &NameStr, BasicBlock *InsertAtEnd)
872 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000873 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000874 if (UnwindDest)
875 ++NumReservedValues;
876 init(ParentPad, UnwindDest, NumReservedValues + 1);
877 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000878}
879
David Majnemer8a1c45d2015-12-12 05:38:55 +0000880CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
881 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
882 CSI.getNumOperands()) {
883 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
884 setNumHungOffUseOperands(ReservedSpace);
885 Use *OL = getOperandList();
886 const Use *InOL = CSI.getOperandList();
887 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
888 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000889}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000890
891void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
892 unsigned NumReservedValues) {
893 assert(ParentPad && NumReservedValues);
894
895 ReservedSpace = NumReservedValues;
896 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
897 allocHungoffUses(ReservedSpace);
898
899 Op<0>() = ParentPad;
900 if (UnwindDest) {
901 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
902 setUnwindDest(UnwindDest);
903 }
904}
905
906/// growOperands - grow operands - This grows the operand list in response to a
907/// push_back style of operation. This grows the number of ops by 2 times.
908void CatchSwitchInst::growOperands(unsigned Size) {
909 unsigned NumOperands = getNumOperands();
910 assert(NumOperands >= 1);
911 if (ReservedSpace >= NumOperands + Size)
912 return;
913 ReservedSpace = (NumOperands + Size / 2) * 2;
914 growHungoffUses(ReservedSpace);
915}
916
917void CatchSwitchInst::addHandler(BasicBlock *Handler) {
918 unsigned OpNo = getNumOperands();
919 growOperands(1);
920 assert(OpNo < ReservedSpace && "Growing didn't work!");
921 setNumHungOffUseOperands(getNumOperands() + 1);
922 getOperandList()[OpNo] = Handler;
923}
924
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000925void CatchSwitchInst::removeHandler(handler_iterator HI) {
926 // Move all subsequent handlers up one.
927 Use *EndDst = op_end() - 1;
928 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
929 *CurDst = *(CurDst + 1);
930 // Null out the last handler use.
931 *EndDst = nullptr;
932
933 setNumHungOffUseOperands(getNumOperands() - 1);
934}
935
David Majnemer8a1c45d2015-12-12 05:38:55 +0000936BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
937 return getSuccessor(idx);
938}
939unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +0000940 return getNumSuccessors();
941}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000942void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
943 setSuccessor(idx, B);
944}
945
946//===----------------------------------------------------------------------===//
947// FuncletPadInst Implementation
948//===----------------------------------------------------------------------===//
949void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
950 const Twine &NameStr) {
951 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
952 std::copy(Args.begin(), Args.end(), op_begin());
953 setParentPad(ParentPad);
954 setName(NameStr);
955}
956
957FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
958 : Instruction(FPI.getType(), FPI.getOpcode(),
959 OperandTraits<FuncletPadInst>::op_end(this) -
960 FPI.getNumOperands(),
961 FPI.getNumOperands()) {
962 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
963 setParentPad(FPI.getParentPad());
964}
965
966FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
967 ArrayRef<Value *> Args, unsigned Values,
968 const Twine &NameStr, Instruction *InsertBefore)
969 : Instruction(ParentPad->getType(), Op,
970 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
971 InsertBefore) {
972 init(ParentPad, Args, NameStr);
973}
974
975FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
976 ArrayRef<Value *> Args, unsigned Values,
977 const Twine &NameStr, BasicBlock *InsertAtEnd)
978 : Instruction(ParentPad->getType(), Op,
979 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
980 InsertAtEnd) {
981 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000982}
983
984//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000985// UnreachableInst Implementation
986//===----------------------------------------------------------------------===//
987
Owen Anderson55f1c092009-08-13 21:58:54 +0000988UnreachableInst::UnreachableInst(LLVMContext &Context,
989 Instruction *InsertBefore)
990 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000991 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000992}
Owen Anderson55f1c092009-08-13 21:58:54 +0000993UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
994 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000995 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000996}
997
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998unsigned UnreachableInst::getNumSuccessorsV() const {
999 return getNumSuccessors();
1000}
1001
1002void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001003 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001004}
1005
1006BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001007 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001008}
1009
1010//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011// BranchInst Implementation
1012//===----------------------------------------------------------------------===//
1013
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014void BranchInst::AssertOK() {
1015 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001016 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001017 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001018}
1019
Chris Lattner2195fc42007-02-24 00:55:48 +00001020BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001021 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001022 OperandTraits<BranchInst>::op_end(this) - 1,
1023 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001024 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001025 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001026}
1027BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1028 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001029 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001030 OperandTraits<BranchInst>::op_end(this) - 3,
1031 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001032 Op<-1>() = IfTrue;
1033 Op<-2>() = IfFalse;
1034 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001035#ifndef NDEBUG
1036 AssertOK();
1037#endif
1038}
1039
1040BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001041 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001042 OperandTraits<BranchInst>::op_end(this) - 1,
1043 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001044 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001045 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001046}
1047
1048BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1049 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001050 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001051 OperandTraits<BranchInst>::op_end(this) - 3,
1052 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001053 Op<-1>() = IfTrue;
1054 Op<-2>() = IfFalse;
1055 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001056#ifndef NDEBUG
1057 AssertOK();
1058#endif
1059}
1060
1061
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001063 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001064 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1065 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001066 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067 if (BI.getNumOperands() != 1) {
1068 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001069 Op<-3>() = BI.Op<-3>();
1070 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001072 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001073}
1074
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001075void BranchInst::swapSuccessors() {
1076 assert(isConditional() &&
1077 "Cannot swap successors of an unconditional branch");
1078 Op<-1>().swap(Op<-2>());
1079
1080 // Update profile metadata if present and it matches our structural
1081 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001082 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001083 if (!ProfileData || ProfileData->getNumOperands() != 3)
1084 return;
1085
1086 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001087 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1088 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001089 setMetadata(LLVMContext::MD_prof,
1090 MDNode::get(ProfileData->getContext(), Ops));
1091}
1092
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001093BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1094 return getSuccessor(idx);
1095}
1096unsigned BranchInst::getNumSuccessorsV() const {
1097 return getNumSuccessors();
1098}
1099void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1100 setSuccessor(idx, B);
1101}
1102
1103
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001104//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001105// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106//===----------------------------------------------------------------------===//
1107
Owen Andersonb6b25302009-07-14 23:09:55 +00001108static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001110 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001111 else {
1112 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001113 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001114 assert(Amt->getType()->isIntegerTy() &&
1115 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001116 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001117 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118}
1119
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001120AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1121 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001122
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001123AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1124 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001125
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001126AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001127 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001128 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001129
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001130AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001131 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001132 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001133
Chris Lattner229907c2011-07-18 04:54:35 +00001134AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001135 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001136 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1137 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1138 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001139 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001140 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001141 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001142}
1143
Chris Lattner229907c2011-07-18 04:54:35 +00001144AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001145 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001146 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1147 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1148 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001149 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001150 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001151 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001152}
1153
Gordon Henriksen14a55692007-12-10 02:14:30 +00001154// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001155AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001156}
1157
Victor Hernandez8acf2952009-10-23 21:09:37 +00001158void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001159 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001160 assert(Align <= MaximumAlignment &&
1161 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001162 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1163 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001164 assert(getAlignment() == Align && "Alignment representation error!");
1165}
1166
Victor Hernandez8acf2952009-10-23 21:09:37 +00001167bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001168 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001169 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001170 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001171}
1172
Chris Lattner8b291e62008-11-26 02:54:17 +00001173/// isStaticAlloca - Return true if this alloca is in the entry block of the
1174/// function and is a constant size. If so, the code generator will fold it
1175/// into the prolog/epilog code, so it is basically free.
1176bool AllocaInst::isStaticAlloca() const {
1177 // Must be constant size.
1178 if (!isa<ConstantInt>(getArraySize())) return false;
1179
1180 // Must be in the entry block.
1181 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001182 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001183}
1184
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001185//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186// LoadInst Implementation
1187//===----------------------------------------------------------------------===//
1188
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001189void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001190 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001192 assert(!(isAtomic() && getAlignment() == 0) &&
1193 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001194}
1195
Daniel Dunbar4975db62009-07-25 04:41:11 +00001196LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001197 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001198
Daniel Dunbar4975db62009-07-25 04:41:11 +00001199LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001200 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001201
David Blaikie0c28fd72015-05-20 21:46:30 +00001202LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001203 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001204 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001205
1206LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1207 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001208 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001209
David Blaikieb7a029872015-04-17 19:56:21 +00001210LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001211 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001212 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001213 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001215LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001216 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001217 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001218}
1219
David Blaikie15d9a4c2015-04-06 20:59:48 +00001220LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001221 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001222 SynchronizationScope SynchScope, Instruction *InsertBef)
1223 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001224 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001225 setVolatile(isVolatile);
1226 setAlignment(Align);
1227 setAtomic(Order, SynchScope);
1228 AssertOK();
1229 setName(Name);
1230}
1231
1232LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1233 unsigned Align, AtomicOrdering Order,
1234 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001235 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001236 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001237 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001238 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001239 setAlignment(Align);
1240 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001241 AssertOK();
1242 setName(Name);
1243}
1244
Daniel Dunbar27096822009-08-11 18:11:15 +00001245LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1246 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1247 Load, Ptr, InsertBef) {
1248 setVolatile(false);
1249 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001250 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001251 AssertOK();
1252 if (Name && Name[0]) setName(Name);
1253}
1254
1255LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1256 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1257 Load, Ptr, InsertAE) {
1258 setVolatile(false);
1259 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001260 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001261 AssertOK();
1262 if (Name && Name[0]) setName(Name);
1263}
1264
David Blaikie0c28fd72015-05-20 21:46:30 +00001265LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001266 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001267 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1268 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001269 setVolatile(isVolatile);
1270 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001271 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001272 AssertOK();
1273 if (Name && Name[0]) setName(Name);
1274}
1275
1276LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1277 BasicBlock *InsertAE)
1278 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1279 Load, Ptr, InsertAE) {
1280 setVolatile(isVolatile);
1281 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001282 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001283 AssertOK();
1284 if (Name && Name[0]) setName(Name);
1285}
1286
Christopher Lamb84485702007-04-22 19:24:39 +00001287void LoadInst::setAlignment(unsigned Align) {
1288 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001289 assert(Align <= MaximumAlignment &&
1290 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001291 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001292 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001293 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001294}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295
1296//===----------------------------------------------------------------------===//
1297// StoreInst Implementation
1298//===----------------------------------------------------------------------===//
1299
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001300void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001301 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001302 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001303 "Ptr must have pointer type!");
1304 assert(getOperand(0)->getType() ==
1305 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001306 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001307 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001308 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001309}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001311StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001312 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001313
1314StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001315 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001316
Misha Brukmanb1c93172005-04-21 23:48:37 +00001317StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001318 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001319 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001320
1321StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001322 BasicBlock *InsertAtEnd)
1323 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1324
1325StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1326 Instruction *InsertBefore)
1327 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1328 InsertBefore) {}
1329
1330StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1331 BasicBlock *InsertAtEnd)
1332 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1333 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001334
Misha Brukmanb1c93172005-04-21 23:48:37 +00001335StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001336 unsigned Align, AtomicOrdering Order,
1337 SynchronizationScope SynchScope,
1338 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001339 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001340 OperandTraits<StoreInst>::op_begin(this),
1341 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001342 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001343 Op<0>() = val;
1344 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001345 setVolatile(isVolatile);
1346 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001347 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001348 AssertOK();
1349}
1350
1351StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001352 unsigned Align, AtomicOrdering Order,
1353 SynchronizationScope SynchScope,
1354 BasicBlock *InsertAtEnd)
1355 : Instruction(Type::getVoidTy(val->getContext()), Store,
1356 OperandTraits<StoreInst>::op_begin(this),
1357 OperandTraits<StoreInst>::operands(this),
1358 InsertAtEnd) {
1359 Op<0>() = val;
1360 Op<1>() = addr;
1361 setVolatile(isVolatile);
1362 setAlignment(Align);
1363 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001364 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365}
1366
Christopher Lamb84485702007-04-22 19:24:39 +00001367void StoreInst::setAlignment(unsigned Align) {
1368 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001369 assert(Align <= MaximumAlignment &&
1370 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001371 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001372 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001373 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001374}
1375
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001376//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001377// AtomicCmpXchgInst Implementation
1378//===----------------------------------------------------------------------===//
1379
1380void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001381 AtomicOrdering SuccessOrdering,
1382 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001383 SynchronizationScope SynchScope) {
1384 Op<0>() = Ptr;
1385 Op<1>() = Cmp;
1386 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001387 setSuccessOrdering(SuccessOrdering);
1388 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001389 setSynchScope(SynchScope);
1390
1391 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1392 "All operands must be non-null!");
1393 assert(getOperand(0)->getType()->isPointerTy() &&
1394 "Ptr must have pointer type!");
1395 assert(getOperand(1)->getType() ==
1396 cast<PointerType>(getOperand(0)->getType())->getElementType()
1397 && "Ptr must be a pointer to Cmp type!");
1398 assert(getOperand(2)->getType() ==
1399 cast<PointerType>(getOperand(0)->getType())->getElementType()
1400 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001401 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001402 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001403 assert(FailureOrdering != NotAtomic &&
1404 "AtomicCmpXchg instructions must be atomic!");
1405 assert(SuccessOrdering >= FailureOrdering &&
1406 "AtomicCmpXchg success ordering must be at least as strong as fail");
1407 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1408 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001409}
1410
1411AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001412 AtomicOrdering SuccessOrdering,
1413 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001414 SynchronizationScope SynchScope,
1415 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001416 : Instruction(
1417 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1418 nullptr),
1419 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1420 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001421 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001422}
1423
1424AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001425 AtomicOrdering SuccessOrdering,
1426 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001427 SynchronizationScope SynchScope,
1428 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001429 : Instruction(
1430 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1431 nullptr),
1432 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1433 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001434 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001435}
Tim Northover420a2162014-06-13 14:24:07 +00001436
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001437//===----------------------------------------------------------------------===//
1438// AtomicRMWInst Implementation
1439//===----------------------------------------------------------------------===//
1440
1441void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1442 AtomicOrdering Ordering,
1443 SynchronizationScope SynchScope) {
1444 Op<0>() = Ptr;
1445 Op<1>() = Val;
1446 setOperation(Operation);
1447 setOrdering(Ordering);
1448 setSynchScope(SynchScope);
1449
1450 assert(getOperand(0) && getOperand(1) &&
1451 "All operands must be non-null!");
1452 assert(getOperand(0)->getType()->isPointerTy() &&
1453 "Ptr must have pointer type!");
1454 assert(getOperand(1)->getType() ==
1455 cast<PointerType>(getOperand(0)->getType())->getElementType()
1456 && "Ptr must be a pointer to Val type!");
1457 assert(Ordering != NotAtomic &&
1458 "AtomicRMW instructions must be atomic!");
1459}
1460
1461AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1462 AtomicOrdering Ordering,
1463 SynchronizationScope SynchScope,
1464 Instruction *InsertBefore)
1465 : Instruction(Val->getType(), AtomicRMW,
1466 OperandTraits<AtomicRMWInst>::op_begin(this),
1467 OperandTraits<AtomicRMWInst>::operands(this),
1468 InsertBefore) {
1469 Init(Operation, Ptr, Val, Ordering, SynchScope);
1470}
1471
1472AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1473 AtomicOrdering Ordering,
1474 SynchronizationScope SynchScope,
1475 BasicBlock *InsertAtEnd)
1476 : Instruction(Val->getType(), AtomicRMW,
1477 OperandTraits<AtomicRMWInst>::op_begin(this),
1478 OperandTraits<AtomicRMWInst>::operands(this),
1479 InsertAtEnd) {
1480 Init(Operation, Ptr, Val, Ordering, SynchScope);
1481}
1482
1483//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001484// FenceInst Implementation
1485//===----------------------------------------------------------------------===//
1486
1487FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1488 SynchronizationScope SynchScope,
1489 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001490 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001491 setOrdering(Ordering);
1492 setSynchScope(SynchScope);
1493}
1494
1495FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1496 SynchronizationScope SynchScope,
1497 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001498 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001499 setOrdering(Ordering);
1500 setSynchScope(SynchScope);
1501}
1502
1503//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001504// GetElementPtrInst Implementation
1505//===----------------------------------------------------------------------===//
1506
Pete Cooper1d078692015-12-14 20:29:16 +00001507void GetElementPtrInst::anchor() {}
1508
Jay Foadd1b78492011-07-25 09:48:08 +00001509void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001510 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001511 assert(getNumOperands() == 1 + IdxList.size() &&
1512 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001513 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001514 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001515 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001516}
1517
Gabor Greiff6caff662008-05-10 08:32:32 +00001518GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001519 : Instruction(GEPI.getType(), GetElementPtr,
1520 OperandTraits<GetElementPtrInst>::op_end(this) -
1521 GEPI.getNumOperands(),
1522 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001523 SourceElementType(GEPI.SourceElementType),
1524 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001525 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001526 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001527}
1528
Chris Lattner6090a422009-03-09 04:46:40 +00001529/// getIndexedType - Returns the type of the element that would be accessed with
1530/// a gep instruction with the specified parameters.
1531///
1532/// The Idxs pointer should point to a continuous piece of memory containing the
1533/// indices, either as Value* or uint64_t.
1534///
1535/// A null type is returned if the indices are invalid for the specified
1536/// pointer type.
1537///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001538template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001539static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001540 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001541 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001542 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001543
Chris Lattner6090a422009-03-09 04:46:40 +00001544 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001545 // it cannot be 'stepped over'.
1546 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001547 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001548
Dan Gohman1ecaf452008-05-31 00:58:22 +00001549 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001550 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001551 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001552 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001553 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001554 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001555 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001556 }
Craig Topperc6207612014-04-09 06:08:46 +00001557 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001558}
1559
David Blaikied288fb82015-03-30 21:41:43 +00001560Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1561 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001562}
1563
David Blaikied288fb82015-03-30 21:41:43 +00001564Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001565 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001566 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001567}
1568
David Blaikied288fb82015-03-30 21:41:43 +00001569Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1570 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001571}
1572
Chris Lattner45f15572007-04-14 00:12:57 +00001573/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1574/// zeros. If so, the result pointer and the first operand have the same
1575/// value, just potentially different types.
1576bool GetElementPtrInst::hasAllZeroIndices() const {
1577 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1578 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1579 if (!CI->isZero()) return false;
1580 } else {
1581 return false;
1582 }
1583 }
1584 return true;
1585}
1586
Chris Lattner27058292007-04-27 20:35:56 +00001587/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1588/// constant integers. If so, the result pointer and the first operand have
1589/// a constant offset between them.
1590bool GetElementPtrInst::hasAllConstantIndices() const {
1591 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1592 if (!isa<ConstantInt>(getOperand(i)))
1593 return false;
1594 }
1595 return true;
1596}
1597
Dan Gohman1b849082009-09-07 23:54:19 +00001598void GetElementPtrInst::setIsInBounds(bool B) {
1599 cast<GEPOperator>(this)->setIsInBounds(B);
1600}
Chris Lattner45f15572007-04-14 00:12:57 +00001601
Nick Lewycky28a5f252009-09-27 21:33:04 +00001602bool GetElementPtrInst::isInBounds() const {
1603 return cast<GEPOperator>(this)->isInBounds();
1604}
1605
Chandler Carruth1e140532012-12-11 10:29:10 +00001606bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1607 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001608 // Delegate to the generic GEPOperator implementation.
1609 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001610}
1611
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001613// ExtractElementInst Implementation
1614//===----------------------------------------------------------------------===//
1615
1616ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001617 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001618 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001619 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001620 ExtractElement,
1621 OperandTraits<ExtractElementInst>::op_begin(this),
1622 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001623 assert(isValidOperands(Val, Index) &&
1624 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001625 Op<0>() = Val;
1626 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001627 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001628}
1629
1630ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001631 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001632 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001633 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001634 ExtractElement,
1635 OperandTraits<ExtractElementInst>::op_begin(this),
1636 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001637 assert(isValidOperands(Val, Index) &&
1638 "Invalid extractelement instruction operands!");
1639
Gabor Greif2d3024d2008-05-26 21:33:52 +00001640 Op<0>() = Val;
1641 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001642 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001643}
1644
Chris Lattner65511ff2006-10-05 06:24:58 +00001645
Chris Lattner54865b32006-04-08 04:05:48 +00001646bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001647 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001648 return false;
1649 return true;
1650}
1651
1652
Robert Bocchino23004482006-01-10 19:05:34 +00001653//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001654// InsertElementInst Implementation
1655//===----------------------------------------------------------------------===//
1656
Chris Lattner54865b32006-04-08 04:05:48 +00001657InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001658 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001659 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001660 : Instruction(Vec->getType(), InsertElement,
1661 OperandTraits<InsertElementInst>::op_begin(this),
1662 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001663 assert(isValidOperands(Vec, Elt, Index) &&
1664 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001665 Op<0>() = Vec;
1666 Op<1>() = Elt;
1667 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001668 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001669}
1670
Chris Lattner54865b32006-04-08 04:05:48 +00001671InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001672 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001673 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001674 : Instruction(Vec->getType(), InsertElement,
1675 OperandTraits<InsertElementInst>::op_begin(this),
1676 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001677 assert(isValidOperands(Vec, Elt, Index) &&
1678 "Invalid insertelement instruction operands!");
1679
Gabor Greif2d3024d2008-05-26 21:33:52 +00001680 Op<0>() = Vec;
1681 Op<1>() = Elt;
1682 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001683 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001684}
1685
Chris Lattner54865b32006-04-08 04:05:48 +00001686bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1687 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001688 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001689 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001690
Reid Spencerd84d35b2007-02-15 02:26:10 +00001691 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001692 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001693
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001694 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001695 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001696 return true;
1697}
1698
1699
Robert Bocchinoca27f032006-01-17 20:07:22 +00001700//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001701// ShuffleVectorInst Implementation
1702//===----------------------------------------------------------------------===//
1703
1704ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001705 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001706 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001707: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001708 cast<VectorType>(Mask->getType())->getNumElements()),
1709 ShuffleVector,
1710 OperandTraits<ShuffleVectorInst>::op_begin(this),
1711 OperandTraits<ShuffleVectorInst>::operands(this),
1712 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001713 assert(isValidOperands(V1, V2, Mask) &&
1714 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001715 Op<0>() = V1;
1716 Op<1>() = V2;
1717 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001718 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001719}
1720
1721ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001722 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001723 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001724: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1725 cast<VectorType>(Mask->getType())->getNumElements()),
1726 ShuffleVector,
1727 OperandTraits<ShuffleVectorInst>::op_begin(this),
1728 OperandTraits<ShuffleVectorInst>::operands(this),
1729 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001730 assert(isValidOperands(V1, V2, Mask) &&
1731 "Invalid shuffle vector instruction operands!");
1732
Gabor Greif2d3024d2008-05-26 21:33:52 +00001733 Op<0>() = V1;
1734 Op<1>() = V2;
1735 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001736 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001737}
1738
Mon P Wang25f01062008-11-10 04:46:22 +00001739bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001740 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001741 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001742 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001743 return false;
1744
Chris Lattner1dcb6542012-01-25 23:49:49 +00001745 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001746 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001747 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001748 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001749
1750 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001751 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1752 return true;
1753
Nate Begeman60a31c32010-08-13 00:16:46 +00001754 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001755 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001756 for (Value *Op : MV->operands()) {
1757 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001758 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001759 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001760 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001761 return false;
1762 }
1763 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001764 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001765 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001766
1767 if (const ConstantDataSequential *CDS =
1768 dyn_cast<ConstantDataSequential>(Mask)) {
1769 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1770 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1771 if (CDS->getElementAsInteger(i) >= V1Size*2)
1772 return false;
1773 return true;
1774 }
1775
1776 // The bitcode reader can create a place holder for a forward reference
1777 // used as the shuffle mask. When this occurs, the shuffle mask will
1778 // fall into this case and fail. To avoid this error, do this bit of
1779 // ugliness to allow such a mask pass.
1780 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1781 if (CE->getOpcode() == Instruction::UserOp1)
1782 return true;
1783
1784 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001785}
1786
Chris Lattnerf724e342008-03-02 05:28:33 +00001787/// getMaskValue - Return the index from the shuffle mask for the specified
1788/// output result. This is either -1 if the element is undef or a number less
1789/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001790int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1791 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1792 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001793 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001794 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001795 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001796 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001797 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001798}
1799
Chris Lattner1dcb6542012-01-25 23:49:49 +00001800/// getShuffleMask - Return the full mask for this instruction, where each
1801/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001802void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1803 SmallVectorImpl<int> &Result) {
1804 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001805
Chris Lattnercf129702012-01-26 02:51:13 +00001806 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001807 for (unsigned i = 0; i != NumElts; ++i)
1808 Result.push_back(CDS->getElementAsInteger(i));
1809 return;
1810 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001811 for (unsigned i = 0; i != NumElts; ++i) {
1812 Constant *C = Mask->getAggregateElement(i);
1813 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001814 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001815 }
1816}
1817
1818
Dan Gohman12fce772008-05-15 19:50:34 +00001819//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001820// InsertValueInst Class
1821//===----------------------------------------------------------------------===//
1822
Jay Foad57aa6362011-07-13 10:26:04 +00001823void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1824 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001825 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001826
1827 // There's no fundamental reason why we require at least one index
1828 // (other than weirdness with &*IdxBegin being invalid; see
1829 // getelementptr's init routine for example). But there's no
1830 // present need to support it.
1831 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1832
1833 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001834 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001835 Op<0>() = Agg;
1836 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001837
Jay Foad57aa6362011-07-13 10:26:04 +00001838 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001839 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001840}
1841
1842InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001843 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001844 OperandTraits<InsertValueInst>::op_begin(this), 2),
1845 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001846 Op<0>() = IVI.getOperand(0);
1847 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001848 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001849}
1850
1851//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001852// ExtractValueInst Class
1853//===----------------------------------------------------------------------===//
1854
Jay Foad57aa6362011-07-13 10:26:04 +00001855void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001856 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001857
Jay Foad57aa6362011-07-13 10:26:04 +00001858 // There's no fundamental reason why we require at least one index.
1859 // But there's no present need to support it.
1860 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001861
Jay Foad57aa6362011-07-13 10:26:04 +00001862 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001863 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001864}
1865
1866ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001867 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001868 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001869 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001870}
1871
Dan Gohman12fce772008-05-15 19:50:34 +00001872// getIndexedType - Returns the type of the element that would be extracted
1873// with an extractvalue instruction with the specified parameters.
1874//
1875// A null type is returned if the indices are invalid for the specified
1876// pointer type.
1877//
Chris Lattner229907c2011-07-18 04:54:35 +00001878Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001879 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001880 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001881 // We can't use CompositeType::indexValid(Index) here.
1882 // indexValid() always returns true for arrays because getelementptr allows
1883 // out-of-bounds indices. Since we don't allow those for extractvalue and
1884 // insertvalue we need to check array indexing manually.
1885 // Since the only other types we can index into are struct types it's just
1886 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001887 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001888 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001889 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001890 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001891 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001892 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001893 } else {
1894 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001895 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001896 }
1897
1898 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001899 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001900 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001901}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001902
1903//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001904// BinaryOperator Class
1905//===----------------------------------------------------------------------===//
1906
Chris Lattner2195fc42007-02-24 00:55:48 +00001907BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001908 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001909 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001910 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001911 OperandTraits<BinaryOperator>::op_begin(this),
1912 OperandTraits<BinaryOperator>::operands(this),
1913 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001914 Op<0>() = S1;
1915 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001916 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001917 setName(Name);
1918}
1919
1920BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001921 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001922 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001923 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001924 OperandTraits<BinaryOperator>::op_begin(this),
1925 OperandTraits<BinaryOperator>::operands(this),
1926 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001927 Op<0>() = S1;
1928 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001929 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001930 setName(Name);
1931}
1932
1933
1934void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001935 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001936 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001937 assert(LHS->getType() == RHS->getType() &&
1938 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001939#ifndef NDEBUG
1940 switch (iType) {
1941 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001942 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001943 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001944 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001945 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001946 "Tried to create an integer operation on a non-integer type!");
1947 break;
1948 case FAdd: case FSub:
1949 case FMul:
1950 assert(getType() == LHS->getType() &&
1951 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001952 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001953 "Tried to create a floating-point operation on a "
1954 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001955 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001956 case UDiv:
1957 case SDiv:
1958 assert(getType() == LHS->getType() &&
1959 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001960 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001961 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001962 "Incorrect operand type (not integer) for S/UDIV");
1963 break;
1964 case FDiv:
1965 assert(getType() == LHS->getType() &&
1966 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001967 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001968 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001969 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001970 case URem:
1971 case SRem:
1972 assert(getType() == LHS->getType() &&
1973 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001974 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001975 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001976 "Incorrect operand type (not integer) for S/UREM");
1977 break;
1978 case FRem:
1979 assert(getType() == LHS->getType() &&
1980 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001981 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001982 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001983 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001984 case Shl:
1985 case LShr:
1986 case AShr:
1987 assert(getType() == LHS->getType() &&
1988 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001989 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001990 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001991 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001992 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001993 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001994 case And: case Or:
1995 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001996 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001997 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001998 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001999 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002000 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002001 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002002 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002003 default:
2004 break;
2005 }
2006#endif
2007}
2008
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002009BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002010 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002011 Instruction *InsertBefore) {
2012 assert(S1->getType() == S2->getType() &&
2013 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002014 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002015}
2016
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002017BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002018 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002019 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002020 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002021 InsertAtEnd->getInstList().push_back(Res);
2022 return Res;
2023}
2024
Dan Gohman5476cfd2009-08-12 16:23:25 +00002025BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002027 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002028 return new BinaryOperator(Instruction::Sub,
2029 zero, Op,
2030 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002031}
2032
Dan Gohman5476cfd2009-08-12 16:23:25 +00002033BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002034 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002035 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002036 return new BinaryOperator(Instruction::Sub,
2037 zero, Op,
2038 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002039}
2040
Dan Gohman4ab44202009-12-18 02:58:50 +00002041BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2042 Instruction *InsertBefore) {
2043 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2044 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2045}
2046
2047BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2048 BasicBlock *InsertAtEnd) {
2049 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2050 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2051}
2052
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002053BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2054 Instruction *InsertBefore) {
2055 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2056 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2057}
2058
2059BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2060 BasicBlock *InsertAtEnd) {
2061 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2062 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2063}
2064
Dan Gohman5476cfd2009-08-12 16:23:25 +00002065BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002066 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002067 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002068 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002069 Op->getType(), Name, InsertBefore);
2070}
2071
Dan Gohman5476cfd2009-08-12 16:23:25 +00002072BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002073 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002074 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002075 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002076 Op->getType(), Name, InsertAtEnd);
2077}
2078
Dan Gohman5476cfd2009-08-12 16:23:25 +00002079BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002080 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002081 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002082 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002083 Op->getType(), Name, InsertBefore);
2084}
2085
Dan Gohman5476cfd2009-08-12 16:23:25 +00002086BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002087 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002088 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002089 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002090 Op->getType(), Name, InsertAtEnd);
2091}
2092
2093
2094// isConstantAllOnes - Helper function for several functions below
2095static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002096 if (const Constant *C = dyn_cast<Constant>(V))
2097 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002098 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002099}
2100
Owen Andersonbb2501b2009-07-13 22:18:28 +00002101bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002102 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2103 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002104 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002105 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002106 return false;
2107}
2108
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002109bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002110 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2111 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002112 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002113 if (!IgnoreZeroSign)
2114 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2115 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2116 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002117 return false;
2118}
2119
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002120bool BinaryOperator::isNot(const Value *V) {
2121 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2122 return (Bop->getOpcode() == Instruction::Xor &&
2123 (isConstantAllOnes(Bop->getOperand(1)) ||
2124 isConstantAllOnes(Bop->getOperand(0))));
2125 return false;
2126}
2127
Chris Lattner2c7d1772005-04-24 07:28:37 +00002128Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002129 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002130}
2131
Chris Lattner2c7d1772005-04-24 07:28:37 +00002132const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2133 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134}
2135
Dan Gohmana5b96452009-06-04 22:49:04 +00002136Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002137 return cast<BinaryOperator>(BinOp)->getOperand(1);
2138}
2139
2140const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2141 return getFNegArgument(const_cast<Value*>(BinOp));
2142}
2143
Chris Lattner2c7d1772005-04-24 07:28:37 +00002144Value *BinaryOperator::getNotArgument(Value *BinOp) {
2145 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2146 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2147 Value *Op0 = BO->getOperand(0);
2148 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002149 if (isConstantAllOnes(Op0)) return Op1;
2150
2151 assert(isConstantAllOnes(Op1));
2152 return Op0;
2153}
2154
Chris Lattner2c7d1772005-04-24 07:28:37 +00002155const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2156 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002157}
2158
2159
2160// swapOperands - Exchange the two operands to this instruction. This
2161// instruction is safe to use on any binary instruction and does not
2162// modify the semantics of the instruction. If the instruction is
2163// order dependent (SetLT f.e.) the opcode is changed.
2164//
2165bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002166 if (!isCommutative())
2167 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002168 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169 return false;
2170}
2171
Dan Gohman1b849082009-09-07 23:54:19 +00002172void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2173 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2174}
2175
2176void BinaryOperator::setHasNoSignedWrap(bool b) {
2177 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2178}
2179
2180void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002181 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002182}
2183
Nick Lewycky28a5f252009-09-27 21:33:04 +00002184bool BinaryOperator::hasNoUnsignedWrap() const {
2185 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2186}
2187
2188bool BinaryOperator::hasNoSignedWrap() const {
2189 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2190}
2191
2192bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002193 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002194}
2195
Sanjay Patela982d992014-09-03 01:06:50 +00002196void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002197 // Copy the wrapping flags.
2198 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2199 setHasNoSignedWrap(OB->hasNoSignedWrap());
2200 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2201 }
2202
2203 // Copy the exact flag.
2204 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2205 setIsExact(PE->isExact());
2206
2207 // Copy the fast-math flags.
2208 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002209 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002210}
2211
Sanjay Patela982d992014-09-03 01:06:50 +00002212void BinaryOperator::andIRFlags(const Value *V) {
2213 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2214 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2215 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2216 }
2217
2218 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2219 setIsExact(isExact() & PE->isExact());
2220
2221 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2222 FastMathFlags FM = getFastMathFlags();
2223 FM &= FP->getFastMathFlags();
2224 copyFastMathFlags(FM);
2225 }
2226}
2227
2228
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002229//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002230// FPMathOperator Class
2231//===----------------------------------------------------------------------===//
2232
2233/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2234/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002235/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002236float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002237 const MDNode *MD =
2238 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002239 if (!MD)
2240 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002241 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002242 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002243}
2244
2245
2246//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002247// CastInst Class
2248//===----------------------------------------------------------------------===//
2249
David Blaikie3a15e142011-12-01 08:00:17 +00002250void CastInst::anchor() {}
2251
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252// Just determine if this cast only deals with integral->integral conversion.
2253bool CastInst::isIntegerCast() const {
2254 switch (getOpcode()) {
2255 default: return false;
2256 case Instruction::ZExt:
2257 case Instruction::SExt:
2258 case Instruction::Trunc:
2259 return true;
2260 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002261 return getOperand(0)->getType()->isIntegerTy() &&
2262 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002264}
2265
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266bool CastInst::isLosslessCast() const {
2267 // Only BitCast can be lossless, exit fast if we're not BitCast
2268 if (getOpcode() != Instruction::BitCast)
2269 return false;
2270
2271 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002272 Type *SrcTy = getOperand(0)->getType();
2273 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274 if (SrcTy == DstTy)
2275 return true;
2276
Reid Spencer8d9336d2006-12-31 05:26:44 +00002277 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002278 if (SrcTy->isPointerTy())
2279 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 return false; // Other types have no identity values
2281}
2282
2283/// This function determines if the CastInst does not require any bits to be
2284/// changed in order to effect the cast. Essentially, it identifies cases where
2285/// no code gen is necessary for the cast, hence the name no-op cast. For
2286/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002287/// # bitcast i32* %x to i8*
2288/// # bitcast <2 x i32> %x to <4 x i16>
2289/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002290/// @brief Determine if the described cast is a no-op.
2291bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002292 Type *SrcTy,
2293 Type *DestTy,
2294 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002295 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002296 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297 case Instruction::Trunc:
2298 case Instruction::ZExt:
2299 case Instruction::SExt:
2300 case Instruction::FPTrunc:
2301 case Instruction::FPExt:
2302 case Instruction::UIToFP:
2303 case Instruction::SIToFP:
2304 case Instruction::FPToUI:
2305 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002306 case Instruction::AddrSpaceCast:
2307 // TODO: Target informations may give a more accurate answer here.
2308 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::BitCast:
2310 return true; // BitCast never modifies bits.
2311 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002312 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002313 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002315 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002316 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 }
2318}
2319
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002320/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002321bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002322 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2323}
2324
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002325bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002326 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002327 if (getOpcode() == Instruction::PtrToInt)
2328 PtrOpTy = getOperand(0)->getType();
2329 else if (getOpcode() == Instruction::IntToPtr)
2330 PtrOpTy = getType();
2331
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002332 Type *IntPtrTy =
2333 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002334
2335 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2336}
2337
2338/// This function determines if a pair of casts can be eliminated and what
2339/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340/// instructions like this:
2341/// * %F = firstOpcode SrcTy %x to MidTy
2342/// * %S = secondOpcode MidTy %F to DstTy
2343/// The function returns a resultOpcode so these two casts can be replaced with:
2344/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002345/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346unsigned CastInst::isEliminableCastPair(
2347 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002348 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2349 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350 // Define the 144 possibilities for these two cast instructions. The values
2351 // in this matrix determine what to do in a given situation and select the
2352 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002353 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 // the following cast properties:
2355 //
2356 // Size Compare Source Destination
2357 // Operator Src ? Size Type Sign Type Sign
2358 // -------- ------------ ------------------- ---------------------
2359 // TRUNC > Integer Any Integral Any
2360 // ZEXT < Integral Unsigned Integer Any
2361 // SEXT < Integral Signed Integer Any
2362 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002363 // FPTOSI n/a FloatPt n/a Integral Signed
2364 // UITOFP n/a Integral Unsigned FloatPt n/a
2365 // SITOFP n/a Integral Signed FloatPt n/a
2366 // FPTRUNC > FloatPt n/a FloatPt n/a
2367 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 // PTRTOINT n/a Pointer n/a Integral Unsigned
2369 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002370 // BITCAST = FirstClass n/a FirstClass n/a
2371 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002372 //
2373 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002374 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2375 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002376 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002377 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002378 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002379 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2382 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002383 // T F F U S F F P I B A -+
2384 // R Z S P P I I T P 2 N T S |
2385 // U E E 2 2 2 2 R E I T C C +- secondOp
2386 // N X X U S F F N X N 2 V V |
2387 // C T T I I P P C T T P T T -+
2388 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002389 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002390 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2391 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2392 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2393 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2394 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002395 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002396 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2397 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2398 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2399 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2400 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002402
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002403 // TODO: This logic could be encoded into the table above and handled in the
2404 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002405 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002406 // merging. However, any pair of bitcasts are allowed.
2407 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2408 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2409 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002410
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002411 // Check if any of the casts convert scalars <-> vectors.
2412 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2413 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2414 if (!AreBothBitcasts)
2415 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416
2417 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2418 [secondOp-Instruction::CastOpsBegin];
2419 switch (ElimCase) {
2420 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002421 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return 0;
2423 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002424 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 return firstOp;
2426 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002427 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return secondOp;
2429 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002430 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002431 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002432 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002433 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 return firstOp;
2435 return 0;
2436 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002437 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002438 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002439 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 return firstOp;
2441 return 0;
2442 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002443 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002444 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002445 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 return secondOp;
2447 return 0;
2448 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002449 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002450 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002451 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452 return secondOp;
2453 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002454 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002455 // Cannot simplify if address spaces are different!
2456 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2457 return 0;
2458
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002459 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002460 // We can still fold this without knowing the actual sizes as long we
2461 // know that the intermediate pointer is the largest possible
2462 // pointer size.
2463 // FIXME: Is this always true?
2464 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002465 return Instruction::BitCast;
2466
2467 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002468 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002469 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002470 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 if (MidSize >= PtrSize)
2472 return Instruction::BitCast;
2473 return 0;
2474 }
2475 case 8: {
2476 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2477 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2478 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002479 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2480 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 if (SrcSize == DstSize)
2482 return Instruction::BitCast;
2483 else if (SrcSize < DstSize)
2484 return firstOp;
2485 return secondOp;
2486 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002487 case 9:
2488 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489 return Instruction::ZExt;
2490 case 10:
2491 // fpext followed by ftrunc is allowed if the bit size returned to is
2492 // the same as the original, in which case its just a bitcast
2493 if (SrcTy == DstTy)
2494 return Instruction::BitCast;
2495 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002496 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002498 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002499 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002500 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002501 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2502 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503 if (SrcSize <= PtrSize && SrcSize == DstSize)
2504 return Instruction::BitCast;
2505 return 0;
2506 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002507 case 12: {
2508 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2509 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2510 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2511 return Instruction::AddrSpaceCast;
2512 return Instruction::BitCast;
2513 }
2514 case 13:
2515 // FIXME: this state can be merged with (1), but the following assert
2516 // is useful to check the correcteness of the sequence due to semantic
2517 // change of bitcast.
2518 assert(
2519 SrcTy->isPtrOrPtrVectorTy() &&
2520 MidTy->isPtrOrPtrVectorTy() &&
2521 DstTy->isPtrOrPtrVectorTy() &&
2522 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2523 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2524 "Illegal addrspacecast, bitcast sequence!");
2525 // Allowed, use first cast's opcode
2526 return firstOp;
2527 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002528 // bitcast, addrspacecast -> addrspacecast if the element type of
2529 // bitcast's source is the same as that of addrspacecast's destination.
2530 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2531 return Instruction::AddrSpaceCast;
2532 return 0;
2533
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002534 case 15:
2535 // FIXME: this state can be merged with (1), but the following assert
2536 // is useful to check the correcteness of the sequence due to semantic
2537 // change of bitcast.
2538 assert(
2539 SrcTy->isIntOrIntVectorTy() &&
2540 MidTy->isPtrOrPtrVectorTy() &&
2541 DstTy->isPtrOrPtrVectorTy() &&
2542 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2543 "Illegal inttoptr, bitcast sequence!");
2544 // Allowed, use first cast's opcode
2545 return firstOp;
2546 case 16:
2547 // FIXME: this state can be merged with (2), but the following assert
2548 // is useful to check the correcteness of the sequence due to semantic
2549 // change of bitcast.
2550 assert(
2551 SrcTy->isPtrOrPtrVectorTy() &&
2552 MidTy->isPtrOrPtrVectorTy() &&
2553 DstTy->isIntOrIntVectorTy() &&
2554 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2555 "Illegal bitcast, ptrtoint sequence!");
2556 // Allowed, use second cast's opcode
2557 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002558 case 17:
2559 // (sitofp (zext x)) -> (uitofp x)
2560 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002562 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002564 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565 default:
Craig Topperc514b542012-02-05 22:14:15 +00002566 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568}
2569
Chris Lattner229907c2011-07-18 04:54:35 +00002570CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002571 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002572 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573 // Construct and return the appropriate CastInst subclass
2574 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002575 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2576 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2577 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2578 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2579 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2580 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2581 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2582 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2583 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2584 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2585 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2586 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2587 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2588 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590}
2591
Chris Lattner229907c2011-07-18 04:54:35 +00002592CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002593 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002594 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 // Construct and return the appropriate CastInst subclass
2596 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002597 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2598 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2599 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2600 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2601 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2602 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2603 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2604 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2605 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2606 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2607 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2608 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2609 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2610 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
Chris Lattner229907c2011-07-18 04:54:35 +00002614CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002616 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002617 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002618 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2619 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002620}
2621
Chris Lattner229907c2011-07-18 04:54:35 +00002622CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002623 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002624 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002625 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002626 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2627 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002628}
2629
Chris Lattner229907c2011-07-18 04:54:35 +00002630CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002632 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002633 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002634 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2635 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002636}
2637
Chris Lattner229907c2011-07-18 04:54:35 +00002638CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002640 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002641 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002642 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2643 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002644}
2645
Chris Lattner229907c2011-07-18 04:54:35 +00002646CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002647 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002648 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002649 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002650 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2651 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002652}
2653
Chris Lattner229907c2011-07-18 04:54:35 +00002654CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002655 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002656 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002657 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002658 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2659 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002660}
2661
Chris Lattner229907c2011-07-18 04:54:35 +00002662CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002664 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002665 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2666 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2667 "Invalid cast");
2668 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002669 assert((!Ty->isVectorTy() ||
2670 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002671 "Invalid cast");
2672
Matt Arsenault065ced92013-07-31 00:17:33 +00002673 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002674 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002675
Matt Arsenault740980e2014-07-14 17:24:35 +00002676 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002677}
2678
2679/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002680CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2681 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002682 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002683 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2684 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002685 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002686 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002687 assert((!Ty->isVectorTy() ||
2688 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002689 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002690
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002691 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002692 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002693
Matt Arsenault740980e2014-07-14 17:24:35 +00002694 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2695}
2696
2697CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2698 Value *S, Type *Ty,
2699 const Twine &Name,
2700 BasicBlock *InsertAtEnd) {
2701 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2702 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2703
2704 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2705 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2706
2707 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2708}
2709
2710CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2711 Value *S, Type *Ty,
2712 const Twine &Name,
2713 Instruction *InsertBefore) {
2714 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2715 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2716
2717 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002718 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2719
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002720 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002721}
2722
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002723CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2724 const Twine &Name,
2725 Instruction *InsertBefore) {
2726 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2727 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2728 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2729 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2730
2731 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2732}
2733
Matt Arsenault740980e2014-07-14 17:24:35 +00002734CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002735 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002736 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002737 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002738 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002739 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2740 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002741 Instruction::CastOps opcode =
2742 (SrcBits == DstBits ? Instruction::BitCast :
2743 (SrcBits > DstBits ? Instruction::Trunc :
2744 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002745 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002746}
2747
Chris Lattner229907c2011-07-18 04:54:35 +00002748CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002749 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002750 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002751 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002752 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002753 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2754 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002755 Instruction::CastOps opcode =
2756 (SrcBits == DstBits ? Instruction::BitCast :
2757 (SrcBits > DstBits ? Instruction::Trunc :
2758 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002759 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002760}
2761
Chris Lattner229907c2011-07-18 04:54:35 +00002762CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002763 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002764 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002765 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002766 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002767 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2768 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002769 Instruction::CastOps opcode =
2770 (SrcBits == DstBits ? Instruction::BitCast :
2771 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002772 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002773}
2774
Chris Lattner229907c2011-07-18 04:54:35 +00002775CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002776 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002777 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002778 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002779 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002780 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2781 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002782 Instruction::CastOps opcode =
2783 (SrcBits == DstBits ? Instruction::BitCast :
2784 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002785 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002786}
2787
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002788// Check whether it is valid to call getCastOpcode for these types.
2789// This routine must be kept in sync with getCastOpcode.
2790bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2791 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2792 return false;
2793
2794 if (SrcTy == DestTy)
2795 return true;
2796
2797 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2798 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2799 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2800 // An element by element cast. Valid if casting the elements is valid.
2801 SrcTy = SrcVecTy->getElementType();
2802 DestTy = DestVecTy->getElementType();
2803 }
2804
2805 // Get the bit sizes, we'll need these
2806 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2807 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2808
2809 // Run through the possibilities ...
2810 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002811 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002812 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002813 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002814 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002815 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002816 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002817 // Casting from something else
2818 return SrcTy->isPointerTy();
2819 }
2820 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2821 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002822 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002823 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002824 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002825 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002826 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002827 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002828 return false;
2829 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002830 if (DestTy->isVectorTy()) // Casting to vector
2831 return DestBits == SrcBits;
2832 if (DestTy->isPointerTy()) { // Casting to pointer
2833 if (SrcTy->isPointerTy()) // Casting from pointer
2834 return true;
2835 return SrcTy->isIntegerTy(); // Casting from integral
2836 }
2837 if (DestTy->isX86_MMXTy()) {
2838 if (SrcTy->isVectorTy())
2839 return DestBits == SrcBits; // 64-bit vector to MMX
2840 return false;
2841 } // Casting to something else
2842 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002843}
2844
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002845bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2846 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2847 return false;
2848
2849 if (SrcTy == DestTy)
2850 return true;
2851
2852 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2853 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2854 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2855 // An element by element cast. Valid if casting the elements is valid.
2856 SrcTy = SrcVecTy->getElementType();
2857 DestTy = DestVecTy->getElementType();
2858 }
2859 }
2860 }
2861
2862 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2863 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2864 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2865 }
2866 }
2867
2868 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2869 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2870
2871 // Could still have vectors of pointers if the number of elements doesn't
2872 // match
2873 if (SrcBits == 0 || DestBits == 0)
2874 return false;
2875
2876 if (SrcBits != DestBits)
2877 return false;
2878
2879 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2880 return false;
2881
2882 return true;
2883}
2884
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002885bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002886 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002887 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2888 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002889 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002890 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2891 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002892 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002893
2894 return isBitCastable(SrcTy, DestTy);
2895}
2896
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002897// Provide a way to get a "cast" where the cast opcode is inferred from the
2898// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002899// logic in the castIsValid function below. This axiom should hold:
2900// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2901// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002902// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002903// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002905CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002906 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2907 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002908
Duncan Sands55e50902008-01-06 10:12:28 +00002909 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2910 "Only first class types are castable!");
2911
Duncan Sandsa8514532011-05-18 07:13:41 +00002912 if (SrcTy == DestTy)
2913 return BitCast;
2914
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002915 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002916 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2917 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002918 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2919 // An element by element cast. Find the appropriate opcode based on the
2920 // element types.
2921 SrcTy = SrcVecTy->getElementType();
2922 DestTy = DestVecTy->getElementType();
2923 }
2924
2925 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002926 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2927 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002928
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002930 if (DestTy->isIntegerTy()) { // Casting to integral
2931 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002932 if (DestBits < SrcBits)
2933 return Trunc; // int -> smaller int
2934 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002935 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002936 return SExt; // signed -> SEXT
2937 else
2938 return ZExt; // unsigned -> ZEXT
2939 } else {
2940 return BitCast; // Same size, No-op cast
2941 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002942 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002943 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002944 return FPToSI; // FP -> sint
2945 else
2946 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002947 } else if (SrcTy->isVectorTy()) {
2948 assert(DestBits == SrcBits &&
2949 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950 return BitCast; // Same size, no-op cast
2951 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002952 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953 "Casting from a value that is not first-class type");
2954 return PtrToInt; // ptr -> int
2955 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002956 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2957 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002958 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959 return SIToFP; // sint -> FP
2960 else
2961 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002962 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002963 if (DestBits < SrcBits) {
2964 return FPTrunc; // FP -> smaller FP
2965 } else if (DestBits > SrcBits) {
2966 return FPExt; // FP -> larger FP
2967 } else {
2968 return BitCast; // same size, no-op cast
2969 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002970 } else if (SrcTy->isVectorTy()) {
2971 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002972 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002973 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002974 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002975 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002976 } else if (DestTy->isVectorTy()) {
2977 assert(DestBits == SrcBits &&
2978 "Illegal cast to vector (wrong type or size)");
2979 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002980 } else if (DestTy->isPointerTy()) {
2981 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002982 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2983 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002985 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002986 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002988 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002989 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002990 if (SrcTy->isVectorTy()) {
2991 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002992 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002993 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002994 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002996 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997}
2998
2999//===----------------------------------------------------------------------===//
3000// CastInst SubClass Constructors
3001//===----------------------------------------------------------------------===//
3002
3003/// Check that the construction parameters for a CastInst are correct. This
3004/// could be broken out into the separate constructors but it is useful to have
3005/// it in one place and to eliminate the redundant code for getting the sizes
3006/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003007bool
Chris Lattner229907c2011-07-18 04:54:35 +00003008CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003009
3010 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003011 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003012
Chris Lattner37bc78a2010-01-26 21:51:43 +00003013 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3014 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003015 return false;
3016
3017 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003018 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3019 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003020
Duncan Sands7f646562011-05-18 09:21:57 +00003021 // If these are vector types, get the lengths of the vectors (using zero for
3022 // scalar types means that checking that vector lengths match also checks that
3023 // scalars are not being converted to vectors or vectors to scalars).
3024 unsigned SrcLength = SrcTy->isVectorTy() ?
3025 cast<VectorType>(SrcTy)->getNumElements() : 0;
3026 unsigned DstLength = DstTy->isVectorTy() ?
3027 cast<VectorType>(DstTy)->getNumElements() : 0;
3028
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029 // Switch on the opcode provided
3030 switch (op) {
3031 default: return false; // This is an input error
3032 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003033 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3034 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003036 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3037 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003038 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003039 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3040 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003041 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003042 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3043 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003044 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003045 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3046 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003047 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003049 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3050 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003052 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003053 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3054 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003056 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003057 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003058 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3059 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3060 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003061 return SrcTy->getScalarType()->isPointerTy() &&
3062 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003063 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003064 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003065 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003066 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3067 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3068 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003069 return SrcTy->getScalarType()->isIntegerTy() &&
3070 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003071 case Instruction::BitCast: {
3072 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3073 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3074
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003075 // BitCast implies a no-op cast of type only. No bits change.
3076 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003077 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003078 return false;
3079
Alp Tokerf907b892013-12-05 05:44:44 +00003080 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003081 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003082 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003083 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3084
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003085 // If both are pointers then the address spaces must match.
3086 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3087 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003088
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003089 // A vector of pointers must have the same number of elements.
3090 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3091 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3092 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3093
3094 return false;
3095 }
3096
3097 return true;
3098 }
3099 case Instruction::AddrSpaceCast: {
3100 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3101 if (!SrcPtrTy)
3102 return false;
3103
3104 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3105 if (!DstPtrTy)
3106 return false;
3107
3108 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3109 return false;
3110
3111 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3112 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3113 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3114
3115 return false;
3116 }
3117
3118 return true;
3119 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003120 }
3121}
3122
3123TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003124 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003126 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127}
3128
3129TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003130 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003131) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003132 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003133}
3134
3135ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003136 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003138 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139}
3140
3141ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003142 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003144 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145}
3146SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003147 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003148) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003149 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150}
3151
Jeff Cohencc08c832006-12-02 02:22:01 +00003152SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003153 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003155 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156}
3157
3158FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003159 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003161 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162}
3163
3164FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003190) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193
3194SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
3200SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
3205
3206FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003207 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210}
3211
3212FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003213 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216}
3217
3218FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003219 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222}
3223
3224FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003225 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228}
3229
3230PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003231 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234}
3235
3236PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003237 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240}
3241
3242IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003243 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003244) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246}
3247
3248IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003249 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003250) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252}
3253
3254BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003255 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003256) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258}
3259
3260BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003261 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003262) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003265
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003266AddrSpaceCastInst::AddrSpaceCastInst(
3267 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3268) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3270}
3271
3272AddrSpaceCastInst::AddrSpaceCastInst(
3273 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3274) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3276}
3277
Chris Lattnerf16dc002006-09-17 19:29:56 +00003278//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003279// CmpInst Classes
3280//===----------------------------------------------------------------------===//
3281
Craig Topper3186c012012-09-23 02:12:10 +00003282void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003283
Craig Topper1c3f2832015-12-15 06:11:33 +00003284CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3285 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003286 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003287 OperandTraits<CmpInst>::op_begin(this),
3288 OperandTraits<CmpInst>::operands(this),
3289 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003290 Op<0>() = LHS;
3291 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003292 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003293 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003294}
Gabor Greiff6caff662008-05-10 08:32:32 +00003295
Craig Topper1c3f2832015-12-15 06:11:33 +00003296CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3297 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003298 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003299 OperandTraits<CmpInst>::op_begin(this),
3300 OperandTraits<CmpInst>::operands(this),
3301 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003302 Op<0>() = LHS;
3303 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003304 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003305 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003306}
3307
3308CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003309CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003310 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003311 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312 if (InsertBefore)
3313 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3314 S1, S2, Name);
3315 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003316 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003318 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319
3320 if (InsertBefore)
3321 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3322 S1, S2, Name);
3323 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003324 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003325 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003326}
3327
3328CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003329CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003330 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003331 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3333 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003334 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003335 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3336 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003337}
3338
3339void CmpInst::swapOperands() {
3340 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3341 IC->swapOperands();
3342 else
3343 cast<FCmpInst>(this)->swapOperands();
3344}
3345
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003346bool CmpInst::isCommutative() const {
3347 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003348 return IC->isCommutative();
3349 return cast<FCmpInst>(this)->isCommutative();
3350}
3351
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003352bool CmpInst::isEquality() const {
3353 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003354 return IC->isEquality();
3355 return cast<FCmpInst>(this)->isEquality();
3356}
3357
3358
Dan Gohman4e724382008-05-31 02:47:54 +00003359CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003360 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003361 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003362 case ICMP_EQ: return ICMP_NE;
3363 case ICMP_NE: return ICMP_EQ;
3364 case ICMP_UGT: return ICMP_ULE;
3365 case ICMP_ULT: return ICMP_UGE;
3366 case ICMP_UGE: return ICMP_ULT;
3367 case ICMP_ULE: return ICMP_UGT;
3368 case ICMP_SGT: return ICMP_SLE;
3369 case ICMP_SLT: return ICMP_SGE;
3370 case ICMP_SGE: return ICMP_SLT;
3371 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003372
Dan Gohman4e724382008-05-31 02:47:54 +00003373 case FCMP_OEQ: return FCMP_UNE;
3374 case FCMP_ONE: return FCMP_UEQ;
3375 case FCMP_OGT: return FCMP_ULE;
3376 case FCMP_OLT: return FCMP_UGE;
3377 case FCMP_OGE: return FCMP_ULT;
3378 case FCMP_OLE: return FCMP_UGT;
3379 case FCMP_UEQ: return FCMP_ONE;
3380 case FCMP_UNE: return FCMP_OEQ;
3381 case FCMP_UGT: return FCMP_OLE;
3382 case FCMP_ULT: return FCMP_OGE;
3383 case FCMP_UGE: return FCMP_OLT;
3384 case FCMP_ULE: return FCMP_OGT;
3385 case FCMP_ORD: return FCMP_UNO;
3386 case FCMP_UNO: return FCMP_ORD;
3387 case FCMP_TRUE: return FCMP_FALSE;
3388 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003389 }
3390}
3391
Pete Cooper1d078692015-12-14 20:29:16 +00003392void ICmpInst::anchor() {}
3393
Reid Spencer266e42b2006-12-23 06:05:41 +00003394ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3395 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003396 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003397 case ICMP_EQ: case ICMP_NE:
3398 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3399 return pred;
3400 case ICMP_UGT: return ICMP_SGT;
3401 case ICMP_ULT: return ICMP_SLT;
3402 case ICMP_UGE: return ICMP_SGE;
3403 case ICMP_ULE: return ICMP_SLE;
3404 }
3405}
3406
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003407ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3408 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003409 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003410 case ICMP_EQ: case ICMP_NE:
3411 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3412 return pred;
3413 case ICMP_SGT: return ICMP_UGT;
3414 case ICMP_SLT: return ICMP_ULT;
3415 case ICMP_SGE: return ICMP_UGE;
3416 case ICMP_SLE: return ICMP_ULE;
3417 }
3418}
3419
Reid Spencer0286bc12007-02-28 22:00:54 +00003420/// Initialize a set of values that all satisfy the condition with C.
3421///
3422ConstantRange
3423ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3424 APInt Lower(C);
3425 APInt Upper(C);
3426 uint32_t BitWidth = C.getBitWidth();
3427 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003428 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003429 case ICmpInst::ICMP_EQ: ++Upper; break;
3430 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003431 case ICmpInst::ICMP_ULT:
3432 Lower = APInt::getMinValue(BitWidth);
3433 // Check for an empty-set condition.
3434 if (Lower == Upper)
3435 return ConstantRange(BitWidth, /*isFullSet=*/false);
3436 break;
3437 case ICmpInst::ICMP_SLT:
3438 Lower = APInt::getSignedMinValue(BitWidth);
3439 // Check for an empty-set condition.
3440 if (Lower == Upper)
3441 return ConstantRange(BitWidth, /*isFullSet=*/false);
3442 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003443 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003444 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003445 // Check for an empty-set condition.
3446 if (Lower == Upper)
3447 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003448 break;
3449 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003450 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003451 // Check for an empty-set condition.
3452 if (Lower == Upper)
3453 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003454 break;
3455 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003456 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003457 // Check for a full-set condition.
3458 if (Lower == Upper)
3459 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003460 break;
3461 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003462 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003463 // Check for a full-set condition.
3464 if (Lower == Upper)
3465 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003466 break;
3467 case ICmpInst::ICMP_UGE:
3468 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003469 // Check for a full-set condition.
3470 if (Lower == Upper)
3471 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003472 break;
3473 case ICmpInst::ICMP_SGE:
3474 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003475 // Check for a full-set condition.
3476 if (Lower == Upper)
3477 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003478 break;
3479 }
3480 return ConstantRange(Lower, Upper);
3481}
3482
Dan Gohman4e724382008-05-31 02:47:54 +00003483CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003484 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003485 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003486 case ICMP_EQ: case ICMP_NE:
3487 return pred;
3488 case ICMP_SGT: return ICMP_SLT;
3489 case ICMP_SLT: return ICMP_SGT;
3490 case ICMP_SGE: return ICMP_SLE;
3491 case ICMP_SLE: return ICMP_SGE;
3492 case ICMP_UGT: return ICMP_ULT;
3493 case ICMP_ULT: return ICMP_UGT;
3494 case ICMP_UGE: return ICMP_ULE;
3495 case ICMP_ULE: return ICMP_UGE;
3496
Reid Spencerd9436b62006-11-20 01:22:35 +00003497 case FCMP_FALSE: case FCMP_TRUE:
3498 case FCMP_OEQ: case FCMP_ONE:
3499 case FCMP_UEQ: case FCMP_UNE:
3500 case FCMP_ORD: case FCMP_UNO:
3501 return pred;
3502 case FCMP_OGT: return FCMP_OLT;
3503 case FCMP_OLT: return FCMP_OGT;
3504 case FCMP_OGE: return FCMP_OLE;
3505 case FCMP_OLE: return FCMP_OGE;
3506 case FCMP_UGT: return FCMP_ULT;
3507 case FCMP_ULT: return FCMP_UGT;
3508 case FCMP_UGE: return FCMP_ULE;
3509 case FCMP_ULE: return FCMP_UGE;
3510 }
3511}
3512
Sanjoy Das6e78b172015-10-22 19:57:34 +00003513CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3514 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3515
3516 switch (pred) {
3517 default:
3518 llvm_unreachable("Unknown predicate!");
3519 case CmpInst::ICMP_ULT:
3520 return CmpInst::ICMP_SLT;
3521 case CmpInst::ICMP_ULE:
3522 return CmpInst::ICMP_SLE;
3523 case CmpInst::ICMP_UGT:
3524 return CmpInst::ICMP_SGT;
3525 case CmpInst::ICMP_UGE:
3526 return CmpInst::ICMP_SGE;
3527 }
3528}
3529
Craig Topper1c3f2832015-12-15 06:11:33 +00003530bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003531 switch (predicate) {
3532 default: return false;
3533 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3534 case ICmpInst::ICMP_UGE: return true;
3535 }
3536}
3537
Craig Topper1c3f2832015-12-15 06:11:33 +00003538bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003539 switch (predicate) {
3540 default: return false;
3541 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3542 case ICmpInst::ICMP_SGE: return true;
3543 }
3544}
3545
Craig Topper1c3f2832015-12-15 06:11:33 +00003546bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003547 switch (predicate) {
3548 default: return false;
3549 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3550 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3551 case FCmpInst::FCMP_ORD: return true;
3552 }
3553}
3554
Craig Topper1c3f2832015-12-15 06:11:33 +00003555bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003556 switch (predicate) {
3557 default: return false;
3558 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3559 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3560 case FCmpInst::FCMP_UNO: return true;
3561 }
3562}
3563
Craig Topper1c3f2832015-12-15 06:11:33 +00003564bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003565 switch(predicate) {
3566 default: return false;
3567 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3568 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3569 }
3570}
3571
Craig Topper1c3f2832015-12-15 06:11:33 +00003572bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003573 switch(predicate) {
3574 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3575 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3576 default: return false;
3577 }
3578}
3579
3580
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003581//===----------------------------------------------------------------------===//
3582// SwitchInst Implementation
3583//===----------------------------------------------------------------------===//
3584
Chris Lattnerbaf00152010-11-17 05:41:46 +00003585void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3586 assert(Value && Default && NumReserved);
3587 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003588 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003589 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003590
Pete Coopereb31b682015-05-21 22:48:54 +00003591 Op<0>() = Value;
3592 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003593}
3594
Chris Lattner2195fc42007-02-24 00:55:48 +00003595/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3596/// switch on and a default destination. The number of additional cases can
3597/// be specified here to make memory allocation more efficient. This
3598/// constructor can also autoinsert before another instruction.
3599SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3600 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003601 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003602 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003603 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003604}
3605
3606/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3607/// switch on and a default destination. The number of additional cases can
3608/// be specified here to make memory allocation more efficient. This
3609/// constructor also autoinserts at the end of the specified BasicBlock.
3610SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3611 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003612 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003613 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003614 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003615}
3616
Misha Brukmanb1c93172005-04-21 23:48:37 +00003617SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003618 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003619 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003620 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003621 Use *OL = getOperandList();
3622 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003623 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003624 OL[i] = InOL[i];
3625 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003626 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003627 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003628}
3629
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003630
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003631/// addCase - Add an entry to the switch instruction...
3632///
Chris Lattner47ac1872005-02-24 05:32:09 +00003633void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003634 unsigned NewCaseIdx = getNumCases();
3635 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003636 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003637 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003638 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003639 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003640 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003641 CaseIt Case(this, NewCaseIdx);
3642 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003643 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003644}
3645
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003646/// removeCase - This method removes the specified case and its successor
3647/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003648void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003649 unsigned idx = i.getCaseIndex();
3650
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003651 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003652
3653 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003654 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003655
Jay Foad14277722011-02-01 09:22:34 +00003656 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003657 if (2 + (idx + 1) * 2 != NumOps) {
3658 OL[2 + idx * 2] = OL[NumOps - 2];
3659 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003660 }
3661
3662 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003663 OL[NumOps-2].set(nullptr);
3664 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003665 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003666}
3667
Jay Foade98f29d2011-04-01 08:00:58 +00003668/// growOperands - grow operands - This grows the operand list in response
3669/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003670///
Jay Foade98f29d2011-04-01 08:00:58 +00003671void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003672 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003673 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003674
3675 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003676 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003677}
3678
3679
3680BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3681 return getSuccessor(idx);
3682}
3683unsigned SwitchInst::getNumSuccessorsV() const {
3684 return getNumSuccessors();
3685}
3686void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3687 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003688}
Chris Lattnerf22be932004-10-15 23:52:53 +00003689
Chris Lattner3ed871f2009-10-27 19:13:16 +00003690//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003691// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003692//===----------------------------------------------------------------------===//
3693
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003694void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003695 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003696 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003697 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003698 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003699 allocHungoffUses(ReservedSpace);
3700
Pete Coopereb31b682015-05-21 22:48:54 +00003701 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003702}
3703
3704
Jay Foade98f29d2011-04-01 08:00:58 +00003705/// growOperands - grow operands - This grows the operand list in response
3706/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003707///
Jay Foade98f29d2011-04-01 08:00:58 +00003708void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003710 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711
3712 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003713 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003714}
3715
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003716IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3717 Instruction *InsertBefore)
3718: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003719 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003720 init(Address, NumCases);
3721}
3722
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003723IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3724 BasicBlock *InsertAtEnd)
3725: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003726 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003727 init(Address, NumCases);
3728}
3729
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003730IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003731 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3732 nullptr, IBI.getNumOperands()) {
3733 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003734 Use *OL = getOperandList();
3735 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003736 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3737 OL[i] = InOL[i];
3738 SubclassOptionalData = IBI.SubclassOptionalData;
3739}
3740
Chris Lattner3ed871f2009-10-27 19:13:16 +00003741/// addDestination - Add a destination.
3742///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003743void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003744 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003745 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003746 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003747 // Initialize some new operands.
3748 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003749 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003750 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751}
3752
3753/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003754/// indirectbr instruction.
3755void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003756 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3757
3758 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003759 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003760
3761 // Replace this value with the last one.
3762 OL[idx+1] = OL[NumOps-1];
3763
3764 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003765 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003766 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003767}
3768
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003769BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003770 return getSuccessor(idx);
3771}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003772unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003773 return getNumSuccessors();
3774}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003775void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003776 setSuccessor(idx, B);
3777}
3778
3779//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003780// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003781//===----------------------------------------------------------------------===//
3782
Chris Lattnerf22be932004-10-15 23:52:53 +00003783// Define these methods here so vtables don't get emitted into every translation
3784// unit that uses these classes.
3785
Pete Cooper75403d72015-06-24 20:22:23 +00003786GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003787 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003788}
3789
Pete Cooper75403d72015-06-24 20:22:23 +00003790BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003791 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003792}
3793
Pete Cooper75403d72015-06-24 20:22:23 +00003794FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003795 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003796}
3797
Pete Cooper75403d72015-06-24 20:22:23 +00003798ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003799 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003800}
3801
Pete Cooper75403d72015-06-24 20:22:23 +00003802ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003803 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003804}
3805
Pete Cooper75403d72015-06-24 20:22:23 +00003806InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003807 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003808}
3809
Pete Cooper75403d72015-06-24 20:22:23 +00003810AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003811 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3812 (Value *)getOperand(0), getAlignment());
3813 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3814 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003815}
3816
Pete Cooper75403d72015-06-24 20:22:23 +00003817LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003818 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3819 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003820}
3821
Pete Cooper75403d72015-06-24 20:22:23 +00003822StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003823 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003824 getAlignment(), getOrdering(), getSynchScope());
3825
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003826}
3827
Pete Cooper75403d72015-06-24 20:22:23 +00003828AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003829 AtomicCmpXchgInst *Result =
3830 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003831 getSuccessOrdering(), getFailureOrdering(),
3832 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003833 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003834 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003835 return Result;
3836}
3837
Pete Cooper75403d72015-06-24 20:22:23 +00003838AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003839 AtomicRMWInst *Result =
3840 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3841 getOrdering(), getSynchScope());
3842 Result->setVolatile(isVolatile());
3843 return Result;
3844}
3845
Pete Cooper75403d72015-06-24 20:22:23 +00003846FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003847 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3848}
3849
Pete Cooper75403d72015-06-24 20:22:23 +00003850TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003851 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003852}
3853
Pete Cooper75403d72015-06-24 20:22:23 +00003854ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003855 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003856}
3857
Pete Cooper75403d72015-06-24 20:22:23 +00003858SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003859 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003860}
3861
Pete Cooper75403d72015-06-24 20:22:23 +00003862FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003863 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003864}
3865
Pete Cooper75403d72015-06-24 20:22:23 +00003866FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003867 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003868}
3869
Pete Cooper75403d72015-06-24 20:22:23 +00003870UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003871 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003872}
3873
Pete Cooper75403d72015-06-24 20:22:23 +00003874SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003875 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876}
3877
Pete Cooper75403d72015-06-24 20:22:23 +00003878FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003879 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003880}
3881
Pete Cooper75403d72015-06-24 20:22:23 +00003882FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003883 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003884}
3885
Pete Cooper75403d72015-06-24 20:22:23 +00003886PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003887 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003888}
3889
Pete Cooper75403d72015-06-24 20:22:23 +00003890IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003891 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003892}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893
Pete Cooper75403d72015-06-24 20:22:23 +00003894BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003895 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003896}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003897
Pete Cooper75403d72015-06-24 20:22:23 +00003898AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003899 return new AddrSpaceCastInst(getOperand(0), getType());
3900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003903 if (hasOperandBundles()) {
3904 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3905 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3906 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003908}
3909
Pete Cooper75403d72015-06-24 20:22:23 +00003910SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003911 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003912}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003913
Pete Cooper75403d72015-06-24 20:22:23 +00003914VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003915 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003916}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003917
Pete Cooper75403d72015-06-24 20:22:23 +00003918ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003919 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003920}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003921
Pete Cooper75403d72015-06-24 20:22:23 +00003922InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003923 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003924}
3925
Pete Cooper75403d72015-06-24 20:22:23 +00003926ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003927 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003928}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003929
Pete Cooper75403d72015-06-24 20:22:23 +00003930PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003931
Pete Cooper75403d72015-06-24 20:22:23 +00003932LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003933 return new LandingPadInst(*this);
3934}
3935
Pete Cooper75403d72015-06-24 20:22:23 +00003936ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003937 return new(getNumOperands()) ReturnInst(*this);
3938}
3939
Pete Cooper75403d72015-06-24 20:22:23 +00003940BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003941 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003942}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943
Pete Cooper75403d72015-06-24 20:22:23 +00003944SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003945
Pete Cooper75403d72015-06-24 20:22:23 +00003946IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003947 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003948}
3949
Pete Cooper75403d72015-06-24 20:22:23 +00003950InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003951 if (hasOperandBundles()) {
3952 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3953 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3954 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003955 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003956}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003957
Pete Cooper75403d72015-06-24 20:22:23 +00003958ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003959
David Majnemer654e1302015-07-31 17:58:14 +00003960CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3961 return new (getNumOperands()) CleanupReturnInst(*this);
3962}
3963
David Majnemer654e1302015-07-31 17:58:14 +00003964CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003965 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003966}
3967
David Majnemer8a1c45d2015-12-12 05:38:55 +00003968CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3969 return new CatchSwitchInst(*this);
3970}
3971
3972FuncletPadInst *FuncletPadInst::cloneImpl() const {
3973 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003974}
3975
Pete Cooper75403d72015-06-24 20:22:23 +00003976UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003977 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003979}