blob: fcc9cc4ab2e60ee40797208464585891a3f21ded [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)
JF Bastien800f87a2016-04-06 21:19:33 +00001212 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1213 CrossThread, 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)
JF Bastien800f87a2016-04-06 21:19:33 +00001217 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1218 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001219
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);
JF Bastien800f87a2016-04-06 21:19:33 +00001250 setAtomic(AtomicOrdering::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);
JF Bastien800f87a2016-04-06 21:19:33 +00001260 setAtomic(AtomicOrdering::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);
JF Bastien800f87a2016-04-06 21:19:33 +00001271 setAtomic(AtomicOrdering::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);
JF Bastien800f87a2016-04-06 21:19:33 +00001282 setAtomic(AtomicOrdering::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)
JF Bastien800f87a2016-04-06 21:19:33 +00001327 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1328 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001329
1330StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1331 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001332 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1333 CrossThread, 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!");
JF Bastien800f87a2016-04-06 21:19:33 +00001401 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001402 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001403 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001404 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001405 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1406 "AtomicCmpXchg failure argument shall be no stronger than the success "
1407 "argument");
1408 assert(FailureOrdering != AtomicOrdering::Release &&
1409 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001410 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001411}
1412
1413AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001414 AtomicOrdering SuccessOrdering,
1415 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001416 SynchronizationScope SynchScope,
1417 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001418 : Instruction(
1419 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1420 nullptr),
1421 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1422 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001423 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001424}
1425
1426AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001427 AtomicOrdering SuccessOrdering,
1428 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001429 SynchronizationScope SynchScope,
1430 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001431 : Instruction(
1432 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1433 nullptr),
1434 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1435 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001436 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001437}
Tim Northover420a2162014-06-13 14:24:07 +00001438
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001439//===----------------------------------------------------------------------===//
1440// AtomicRMWInst Implementation
1441//===----------------------------------------------------------------------===//
1442
1443void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1444 AtomicOrdering Ordering,
1445 SynchronizationScope SynchScope) {
1446 Op<0>() = Ptr;
1447 Op<1>() = Val;
1448 setOperation(Operation);
1449 setOrdering(Ordering);
1450 setSynchScope(SynchScope);
1451
1452 assert(getOperand(0) && getOperand(1) &&
1453 "All operands must be non-null!");
1454 assert(getOperand(0)->getType()->isPointerTy() &&
1455 "Ptr must have pointer type!");
1456 assert(getOperand(1)->getType() ==
1457 cast<PointerType>(getOperand(0)->getType())->getElementType()
1458 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001459 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001460 "AtomicRMW instructions must be atomic!");
1461}
1462
1463AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1464 AtomicOrdering Ordering,
1465 SynchronizationScope SynchScope,
1466 Instruction *InsertBefore)
1467 : Instruction(Val->getType(), AtomicRMW,
1468 OperandTraits<AtomicRMWInst>::op_begin(this),
1469 OperandTraits<AtomicRMWInst>::operands(this),
1470 InsertBefore) {
1471 Init(Operation, Ptr, Val, Ordering, SynchScope);
1472}
1473
1474AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1475 AtomicOrdering Ordering,
1476 SynchronizationScope SynchScope,
1477 BasicBlock *InsertAtEnd)
1478 : Instruction(Val->getType(), AtomicRMW,
1479 OperandTraits<AtomicRMWInst>::op_begin(this),
1480 OperandTraits<AtomicRMWInst>::operands(this),
1481 InsertAtEnd) {
1482 Init(Operation, Ptr, Val, Ordering, SynchScope);
1483}
1484
1485//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001486// FenceInst Implementation
1487//===----------------------------------------------------------------------===//
1488
1489FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1490 SynchronizationScope SynchScope,
1491 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001492 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001493 setOrdering(Ordering);
1494 setSynchScope(SynchScope);
1495}
1496
1497FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1498 SynchronizationScope SynchScope,
1499 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001500 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001501 setOrdering(Ordering);
1502 setSynchScope(SynchScope);
1503}
1504
1505//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001506// GetElementPtrInst Implementation
1507//===----------------------------------------------------------------------===//
1508
Pete Cooper1d078692015-12-14 20:29:16 +00001509void GetElementPtrInst::anchor() {}
1510
Jay Foadd1b78492011-07-25 09:48:08 +00001511void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001512 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001513 assert(getNumOperands() == 1 + IdxList.size() &&
1514 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001515 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001516 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001517 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001518}
1519
Gabor Greiff6caff662008-05-10 08:32:32 +00001520GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001521 : Instruction(GEPI.getType(), GetElementPtr,
1522 OperandTraits<GetElementPtrInst>::op_end(this) -
1523 GEPI.getNumOperands(),
1524 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001525 SourceElementType(GEPI.SourceElementType),
1526 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001527 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001528 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001529}
1530
Chris Lattner6090a422009-03-09 04:46:40 +00001531/// getIndexedType - Returns the type of the element that would be accessed with
1532/// a gep instruction with the specified parameters.
1533///
1534/// The Idxs pointer should point to a continuous piece of memory containing the
1535/// indices, either as Value* or uint64_t.
1536///
1537/// A null type is returned if the indices are invalid for the specified
1538/// pointer type.
1539///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001540template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001541static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001542 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001543 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001544 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001545
Chris Lattner6090a422009-03-09 04:46:40 +00001546 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001547 // it cannot be 'stepped over'.
1548 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001549 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001550
Dan Gohman1ecaf452008-05-31 00:58:22 +00001551 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001552 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001553 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001554 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001555 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001556 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001557 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001558 }
Craig Topperc6207612014-04-09 06:08:46 +00001559 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560}
1561
David Blaikied288fb82015-03-30 21:41:43 +00001562Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1563 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001564}
1565
David Blaikied288fb82015-03-30 21:41:43 +00001566Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001567 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001568 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001569}
1570
David Blaikied288fb82015-03-30 21:41:43 +00001571Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1572 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001573}
1574
Chris Lattner45f15572007-04-14 00:12:57 +00001575/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1576/// zeros. If so, the result pointer and the first operand have the same
1577/// value, just potentially different types.
1578bool GetElementPtrInst::hasAllZeroIndices() const {
1579 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1580 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1581 if (!CI->isZero()) return false;
1582 } else {
1583 return false;
1584 }
1585 }
1586 return true;
1587}
1588
Chris Lattner27058292007-04-27 20:35:56 +00001589/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1590/// constant integers. If so, the result pointer and the first operand have
1591/// a constant offset between them.
1592bool GetElementPtrInst::hasAllConstantIndices() const {
1593 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1594 if (!isa<ConstantInt>(getOperand(i)))
1595 return false;
1596 }
1597 return true;
1598}
1599
Dan Gohman1b849082009-09-07 23:54:19 +00001600void GetElementPtrInst::setIsInBounds(bool B) {
1601 cast<GEPOperator>(this)->setIsInBounds(B);
1602}
Chris Lattner45f15572007-04-14 00:12:57 +00001603
Nick Lewycky28a5f252009-09-27 21:33:04 +00001604bool GetElementPtrInst::isInBounds() const {
1605 return cast<GEPOperator>(this)->isInBounds();
1606}
1607
Chandler Carruth1e140532012-12-11 10:29:10 +00001608bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1609 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001610 // Delegate to the generic GEPOperator implementation.
1611 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001612}
1613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001615// ExtractElementInst Implementation
1616//===----------------------------------------------------------------------===//
1617
1618ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001619 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001620 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001621 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001622 ExtractElement,
1623 OperandTraits<ExtractElementInst>::op_begin(this),
1624 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001625 assert(isValidOperands(Val, Index) &&
1626 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001627 Op<0>() = Val;
1628 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001629 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001630}
1631
1632ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001633 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001634 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001635 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001636 ExtractElement,
1637 OperandTraits<ExtractElementInst>::op_begin(this),
1638 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001639 assert(isValidOperands(Val, Index) &&
1640 "Invalid extractelement instruction operands!");
1641
Gabor Greif2d3024d2008-05-26 21:33:52 +00001642 Op<0>() = Val;
1643 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001644 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001645}
1646
Chris Lattner65511ff2006-10-05 06:24:58 +00001647
Chris Lattner54865b32006-04-08 04:05:48 +00001648bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001649 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001650 return false;
1651 return true;
1652}
1653
1654
Robert Bocchino23004482006-01-10 19:05:34 +00001655//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001656// InsertElementInst Implementation
1657//===----------------------------------------------------------------------===//
1658
Chris Lattner54865b32006-04-08 04:05:48 +00001659InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001660 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001661 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001662 : Instruction(Vec->getType(), InsertElement,
1663 OperandTraits<InsertElementInst>::op_begin(this),
1664 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001665 assert(isValidOperands(Vec, Elt, Index) &&
1666 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001667 Op<0>() = Vec;
1668 Op<1>() = Elt;
1669 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001670 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001671}
1672
Chris Lattner54865b32006-04-08 04:05:48 +00001673InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001674 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001675 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001676 : Instruction(Vec->getType(), InsertElement,
1677 OperandTraits<InsertElementInst>::op_begin(this),
1678 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001679 assert(isValidOperands(Vec, Elt, Index) &&
1680 "Invalid insertelement instruction operands!");
1681
Gabor Greif2d3024d2008-05-26 21:33:52 +00001682 Op<0>() = Vec;
1683 Op<1>() = Elt;
1684 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001685 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001686}
1687
Chris Lattner54865b32006-04-08 04:05:48 +00001688bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1689 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001690 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001691 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001692
Reid Spencerd84d35b2007-02-15 02:26:10 +00001693 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001694 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001695
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001696 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001697 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001698 return true;
1699}
1700
1701
Robert Bocchinoca27f032006-01-17 20:07:22 +00001702//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001703// ShuffleVectorInst Implementation
1704//===----------------------------------------------------------------------===//
1705
1706ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001707 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001708 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001709: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001710 cast<VectorType>(Mask->getType())->getNumElements()),
1711 ShuffleVector,
1712 OperandTraits<ShuffleVectorInst>::op_begin(this),
1713 OperandTraits<ShuffleVectorInst>::operands(this),
1714 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001715 assert(isValidOperands(V1, V2, Mask) &&
1716 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001717 Op<0>() = V1;
1718 Op<1>() = V2;
1719 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001720 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001721}
1722
1723ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001724 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001725 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001726: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1727 cast<VectorType>(Mask->getType())->getNumElements()),
1728 ShuffleVector,
1729 OperandTraits<ShuffleVectorInst>::op_begin(this),
1730 OperandTraits<ShuffleVectorInst>::operands(this),
1731 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001732 assert(isValidOperands(V1, V2, Mask) &&
1733 "Invalid shuffle vector instruction operands!");
1734
Gabor Greif2d3024d2008-05-26 21:33:52 +00001735 Op<0>() = V1;
1736 Op<1>() = V2;
1737 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001738 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001739}
1740
Mon P Wang25f01062008-11-10 04:46:22 +00001741bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001742 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001743 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001744 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001745 return false;
1746
Chris Lattner1dcb6542012-01-25 23:49:49 +00001747 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001748 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001749 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001750 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001751
1752 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001753 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1754 return true;
1755
Nate Begeman60a31c32010-08-13 00:16:46 +00001756 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001757 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001758 for (Value *Op : MV->operands()) {
1759 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001760 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001761 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001762 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001763 return false;
1764 }
1765 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001766 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001767 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001768
1769 if (const ConstantDataSequential *CDS =
1770 dyn_cast<ConstantDataSequential>(Mask)) {
1771 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1772 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1773 if (CDS->getElementAsInteger(i) >= V1Size*2)
1774 return false;
1775 return true;
1776 }
1777
1778 // The bitcode reader can create a place holder for a forward reference
1779 // used as the shuffle mask. When this occurs, the shuffle mask will
1780 // fall into this case and fail. To avoid this error, do this bit of
1781 // ugliness to allow such a mask pass.
1782 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1783 if (CE->getOpcode() == Instruction::UserOp1)
1784 return true;
1785
1786 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001787}
1788
Chris Lattnerf724e342008-03-02 05:28:33 +00001789/// getMaskValue - Return the index from the shuffle mask for the specified
1790/// output result. This is either -1 if the element is undef or a number less
1791/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001792int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1793 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1794 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001795 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001796 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001797 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001798 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001799 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001800}
1801
Chris Lattner1dcb6542012-01-25 23:49:49 +00001802/// getShuffleMask - Return the full mask for this instruction, where each
1803/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001804void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1805 SmallVectorImpl<int> &Result) {
1806 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001807
Chris Lattnercf129702012-01-26 02:51:13 +00001808 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001809 for (unsigned i = 0; i != NumElts; ++i)
1810 Result.push_back(CDS->getElementAsInteger(i));
1811 return;
1812 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001813 for (unsigned i = 0; i != NumElts; ++i) {
1814 Constant *C = Mask->getAggregateElement(i);
1815 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001816 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001817 }
1818}
1819
1820
Dan Gohman12fce772008-05-15 19:50:34 +00001821//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001822// InsertValueInst Class
1823//===----------------------------------------------------------------------===//
1824
Jay Foad57aa6362011-07-13 10:26:04 +00001825void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1826 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001827 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001828
1829 // There's no fundamental reason why we require at least one index
1830 // (other than weirdness with &*IdxBegin being invalid; see
1831 // getelementptr's init routine for example). But there's no
1832 // present need to support it.
1833 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1834
1835 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001836 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001837 Op<0>() = Agg;
1838 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001839
Jay Foad57aa6362011-07-13 10:26:04 +00001840 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001841 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001842}
1843
1844InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001845 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001846 OperandTraits<InsertValueInst>::op_begin(this), 2),
1847 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001848 Op<0>() = IVI.getOperand(0);
1849 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001850 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001851}
1852
1853//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001854// ExtractValueInst Class
1855//===----------------------------------------------------------------------===//
1856
Jay Foad57aa6362011-07-13 10:26:04 +00001857void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001858 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001859
Jay Foad57aa6362011-07-13 10:26:04 +00001860 // There's no fundamental reason why we require at least one index.
1861 // But there's no present need to support it.
1862 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001863
Jay Foad57aa6362011-07-13 10:26:04 +00001864 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001865 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001866}
1867
1868ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001869 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001870 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001871 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001872}
1873
Dan Gohman12fce772008-05-15 19:50:34 +00001874// getIndexedType - Returns the type of the element that would be extracted
1875// with an extractvalue instruction with the specified parameters.
1876//
1877// A null type is returned if the indices are invalid for the specified
1878// pointer type.
1879//
Chris Lattner229907c2011-07-18 04:54:35 +00001880Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001881 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001882 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001883 // We can't use CompositeType::indexValid(Index) here.
1884 // indexValid() always returns true for arrays because getelementptr allows
1885 // out-of-bounds indices. Since we don't allow those for extractvalue and
1886 // insertvalue we need to check array indexing manually.
1887 // Since the only other types we can index into are struct types it's just
1888 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001889 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001890 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001891 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001892 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001893 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001894 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001895 } else {
1896 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001897 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001898 }
1899
1900 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001901 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001902 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001903}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001904
1905//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001906// BinaryOperator Class
1907//===----------------------------------------------------------------------===//
1908
Chris Lattner2195fc42007-02-24 00:55:48 +00001909BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001910 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001911 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001912 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001913 OperandTraits<BinaryOperator>::op_begin(this),
1914 OperandTraits<BinaryOperator>::operands(this),
1915 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001916 Op<0>() = S1;
1917 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001918 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001919 setName(Name);
1920}
1921
1922BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001923 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001924 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001925 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001926 OperandTraits<BinaryOperator>::op_begin(this),
1927 OperandTraits<BinaryOperator>::operands(this),
1928 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001929 Op<0>() = S1;
1930 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001931 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001932 setName(Name);
1933}
1934
1935
1936void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001937 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001938 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001939 assert(LHS->getType() == RHS->getType() &&
1940 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001941#ifndef NDEBUG
1942 switch (iType) {
1943 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001944 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001945 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001946 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001947 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001948 "Tried to create an integer operation on a non-integer type!");
1949 break;
1950 case FAdd: case FSub:
1951 case FMul:
1952 assert(getType() == LHS->getType() &&
1953 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001954 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001955 "Tried to create a floating-point operation on a "
1956 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001957 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001958 case UDiv:
1959 case SDiv:
1960 assert(getType() == LHS->getType() &&
1961 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001962 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001963 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001964 "Incorrect operand type (not integer) for S/UDIV");
1965 break;
1966 case FDiv:
1967 assert(getType() == LHS->getType() &&
1968 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001969 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001970 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001971 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001972 case URem:
1973 case SRem:
1974 assert(getType() == LHS->getType() &&
1975 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001976 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001977 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001978 "Incorrect operand type (not integer) for S/UREM");
1979 break;
1980 case FRem:
1981 assert(getType() == LHS->getType() &&
1982 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001983 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001984 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001985 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001986 case Shl:
1987 case LShr:
1988 case AShr:
1989 assert(getType() == LHS->getType() &&
1990 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001991 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001992 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001993 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001994 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001995 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001996 case And: case Or:
1997 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001998 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001999 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002000 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002001 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002002 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002003 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002004 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002005 default:
2006 break;
2007 }
2008#endif
2009}
2010
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002011BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002012 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002013 Instruction *InsertBefore) {
2014 assert(S1->getType() == S2->getType() &&
2015 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002016 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002017}
2018
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002019BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002020 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002021 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002023 InsertAtEnd->getInstList().push_back(Res);
2024 return Res;
2025}
2026
Dan Gohman5476cfd2009-08-12 16:23:25 +00002027BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002028 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002029 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002030 return new BinaryOperator(Instruction::Sub,
2031 zero, Op,
2032 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002033}
2034
Dan Gohman5476cfd2009-08-12 16:23:25 +00002035BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002037 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002038 return new BinaryOperator(Instruction::Sub,
2039 zero, Op,
2040 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002041}
2042
Dan Gohman4ab44202009-12-18 02:58:50 +00002043BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2044 Instruction *InsertBefore) {
2045 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2046 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2047}
2048
2049BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2050 BasicBlock *InsertAtEnd) {
2051 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2052 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2053}
2054
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002055BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2056 Instruction *InsertBefore) {
2057 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2058 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2059}
2060
2061BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2062 BasicBlock *InsertAtEnd) {
2063 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2064 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2065}
2066
Dan Gohman5476cfd2009-08-12 16:23:25 +00002067BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002068 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002069 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002070 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002071 Op->getType(), Name, InsertBefore);
2072}
2073
Dan Gohman5476cfd2009-08-12 16:23:25 +00002074BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002075 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002076 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002077 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002078 Op->getType(), Name, InsertAtEnd);
2079}
2080
Dan Gohman5476cfd2009-08-12 16:23:25 +00002081BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002082 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002083 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002084 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002085 Op->getType(), Name, InsertBefore);
2086}
2087
Dan Gohman5476cfd2009-08-12 16:23:25 +00002088BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002089 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002090 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002091 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002092 Op->getType(), Name, InsertAtEnd);
2093}
2094
2095
2096// isConstantAllOnes - Helper function for several functions below
2097static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002098 if (const Constant *C = dyn_cast<Constant>(V))
2099 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002100 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002101}
2102
Owen Andersonbb2501b2009-07-13 22:18:28 +00002103bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002104 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2105 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002106 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002107 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002108 return false;
2109}
2110
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002111bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002112 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2113 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002114 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002115 if (!IgnoreZeroSign)
2116 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2117 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2118 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002119 return false;
2120}
2121
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002122bool BinaryOperator::isNot(const Value *V) {
2123 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2124 return (Bop->getOpcode() == Instruction::Xor &&
2125 (isConstantAllOnes(Bop->getOperand(1)) ||
2126 isConstantAllOnes(Bop->getOperand(0))));
2127 return false;
2128}
2129
Chris Lattner2c7d1772005-04-24 07:28:37 +00002130Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002131 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002132}
2133
Chris Lattner2c7d1772005-04-24 07:28:37 +00002134const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2135 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002136}
2137
Dan Gohmana5b96452009-06-04 22:49:04 +00002138Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002139 return cast<BinaryOperator>(BinOp)->getOperand(1);
2140}
2141
2142const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2143 return getFNegArgument(const_cast<Value*>(BinOp));
2144}
2145
Chris Lattner2c7d1772005-04-24 07:28:37 +00002146Value *BinaryOperator::getNotArgument(Value *BinOp) {
2147 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2148 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2149 Value *Op0 = BO->getOperand(0);
2150 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002151 if (isConstantAllOnes(Op0)) return Op1;
2152
2153 assert(isConstantAllOnes(Op1));
2154 return Op0;
2155}
2156
Chris Lattner2c7d1772005-04-24 07:28:37 +00002157const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2158 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002159}
2160
2161
2162// swapOperands - Exchange the two operands to this instruction. This
2163// instruction is safe to use on any binary instruction and does not
2164// modify the semantics of the instruction. If the instruction is
2165// order dependent (SetLT f.e.) the opcode is changed.
2166//
2167bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002168 if (!isCommutative())
2169 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002170 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002171 return false;
2172}
2173
Dan Gohman1b849082009-09-07 23:54:19 +00002174void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2175 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2176}
2177
2178void BinaryOperator::setHasNoSignedWrap(bool b) {
2179 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2180}
2181
2182void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002183 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002184}
2185
Nick Lewycky28a5f252009-09-27 21:33:04 +00002186bool BinaryOperator::hasNoUnsignedWrap() const {
2187 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2188}
2189
2190bool BinaryOperator::hasNoSignedWrap() const {
2191 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2192}
2193
2194bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002195 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002196}
2197
Sanjay Patela982d992014-09-03 01:06:50 +00002198void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002199 // Copy the wrapping flags.
2200 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2201 setHasNoSignedWrap(OB->hasNoSignedWrap());
2202 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2203 }
2204
2205 // Copy the exact flag.
2206 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2207 setIsExact(PE->isExact());
2208
2209 // Copy the fast-math flags.
2210 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002211 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002212}
2213
Sanjay Patela982d992014-09-03 01:06:50 +00002214void BinaryOperator::andIRFlags(const Value *V) {
2215 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2216 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2217 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2218 }
2219
2220 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2221 setIsExact(isExact() & PE->isExact());
2222
2223 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2224 FastMathFlags FM = getFastMathFlags();
2225 FM &= FP->getFastMathFlags();
2226 copyFastMathFlags(FM);
2227 }
2228}
2229
2230
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002231//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002232// FPMathOperator Class
2233//===----------------------------------------------------------------------===//
2234
2235/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2236/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002237/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002238float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002239 const MDNode *MD =
2240 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002241 if (!MD)
2242 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002243 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002244 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002245}
2246
2247
2248//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002249// CastInst Class
2250//===----------------------------------------------------------------------===//
2251
David Blaikie3a15e142011-12-01 08:00:17 +00002252void CastInst::anchor() {}
2253
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254// Just determine if this cast only deals with integral->integral conversion.
2255bool CastInst::isIntegerCast() const {
2256 switch (getOpcode()) {
2257 default: return false;
2258 case Instruction::ZExt:
2259 case Instruction::SExt:
2260 case Instruction::Trunc:
2261 return true;
2262 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002263 return getOperand(0)->getType()->isIntegerTy() &&
2264 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002266}
2267
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268bool CastInst::isLosslessCast() const {
2269 // Only BitCast can be lossless, exit fast if we're not BitCast
2270 if (getOpcode() != Instruction::BitCast)
2271 return false;
2272
2273 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002274 Type *SrcTy = getOperand(0)->getType();
2275 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276 if (SrcTy == DstTy)
2277 return true;
2278
Reid Spencer8d9336d2006-12-31 05:26:44 +00002279 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002280 if (SrcTy->isPointerTy())
2281 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 return false; // Other types have no identity values
2283}
2284
2285/// This function determines if the CastInst does not require any bits to be
2286/// changed in order to effect the cast. Essentially, it identifies cases where
2287/// no code gen is necessary for the cast, hence the name no-op cast. For
2288/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002289/// # bitcast i32* %x to i8*
2290/// # bitcast <2 x i32> %x to <4 x i16>
2291/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002292/// @brief Determine if the described cast is a no-op.
2293bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002294 Type *SrcTy,
2295 Type *DestTy,
2296 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002297 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002298 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 case Instruction::Trunc:
2300 case Instruction::ZExt:
2301 case Instruction::SExt:
2302 case Instruction::FPTrunc:
2303 case Instruction::FPExt:
2304 case Instruction::UIToFP:
2305 case Instruction::SIToFP:
2306 case Instruction::FPToUI:
2307 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002308 case Instruction::AddrSpaceCast:
2309 // TODO: Target informations may give a more accurate answer here.
2310 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::BitCast:
2312 return true; // BitCast never modifies bits.
2313 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002314 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002315 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002317 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002318 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319 }
2320}
2321
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002322/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002323bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002324 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2325}
2326
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002327bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002328 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002329 if (getOpcode() == Instruction::PtrToInt)
2330 PtrOpTy = getOperand(0)->getType();
2331 else if (getOpcode() == Instruction::IntToPtr)
2332 PtrOpTy = getType();
2333
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002334 Type *IntPtrTy =
2335 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002336
2337 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2338}
2339
2340/// This function determines if a pair of casts can be eliminated and what
2341/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342/// instructions like this:
2343/// * %F = firstOpcode SrcTy %x to MidTy
2344/// * %S = secondOpcode MidTy %F to DstTy
2345/// The function returns a resultOpcode so these two casts can be replaced with:
2346/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002347/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348unsigned CastInst::isEliminableCastPair(
2349 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002350 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2351 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 // Define the 144 possibilities for these two cast instructions. The values
2353 // in this matrix determine what to do in a given situation and select the
2354 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002355 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 // the following cast properties:
2357 //
2358 // Size Compare Source Destination
2359 // Operator Src ? Size Type Sign Type Sign
2360 // -------- ------------ ------------------- ---------------------
2361 // TRUNC > Integer Any Integral Any
2362 // ZEXT < Integral Unsigned Integer Any
2363 // SEXT < Integral Signed Integer Any
2364 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002365 // FPTOSI n/a FloatPt n/a Integral Signed
2366 // UITOFP n/a Integral Unsigned FloatPt n/a
2367 // SITOFP n/a Integral Signed FloatPt n/a
2368 // FPTRUNC > FloatPt n/a FloatPt n/a
2369 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 // PTRTOINT n/a Pointer n/a Integral Unsigned
2371 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002372 // BITCAST = FirstClass n/a FirstClass n/a
2373 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002374 //
2375 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002376 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2377 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002378 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002379 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002380 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002381 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002382 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2384 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002385 // T F F U S F F P I B A -+
2386 // R Z S P P I I T P 2 N T S |
2387 // U E E 2 2 2 2 R E I T C C +- secondOp
2388 // N X X U S F F N X N 2 V V |
2389 // C T T I I P P C T T P T T -+
2390 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002391 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002392 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2393 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2394 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2395 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2396 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002397 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002398 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2399 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2400 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2401 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2402 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002404
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002405 // TODO: This logic could be encoded into the table above and handled in the
2406 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002407 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002408 // merging. However, any pair of bitcasts are allowed.
2409 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2410 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2411 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002412
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002413 // Check if any of the casts convert scalars <-> vectors.
2414 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2415 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2416 if (!AreBothBitcasts)
2417 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418
2419 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2420 [secondOp-Instruction::CastOpsBegin];
2421 switch (ElimCase) {
2422 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002423 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 return 0;
2425 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002426 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 return firstOp;
2428 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002429 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 return secondOp;
2431 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002432 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002433 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002434 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002435 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 return firstOp;
2437 return 0;
2438 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002440 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002441 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 return firstOp;
2443 return 0;
2444 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002445 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002446 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002447 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448 return secondOp;
2449 return 0;
2450 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002451 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002452 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002453 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 return secondOp;
2455 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002456 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002457 // Cannot simplify if address spaces are different!
2458 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2459 return 0;
2460
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002461 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002462 // We can still fold this without knowing the actual sizes as long we
2463 // know that the intermediate pointer is the largest possible
2464 // pointer size.
2465 // FIXME: Is this always true?
2466 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002467 return Instruction::BitCast;
2468
2469 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002470 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002471 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002472 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 if (MidSize >= PtrSize)
2474 return Instruction::BitCast;
2475 return 0;
2476 }
2477 case 8: {
2478 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2479 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2480 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002481 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2482 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 if (SrcSize == DstSize)
2484 return Instruction::BitCast;
2485 else if (SrcSize < DstSize)
2486 return firstOp;
2487 return secondOp;
2488 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002489 case 9:
2490 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491 return Instruction::ZExt;
2492 case 10:
2493 // fpext followed by ftrunc is allowed if the bit size returned to is
2494 // the same as the original, in which case its just a bitcast
2495 if (SrcTy == DstTy)
2496 return Instruction::BitCast;
2497 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002498 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002500 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002501 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002502 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002503 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2504 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 if (SrcSize <= PtrSize && SrcSize == DstSize)
2506 return Instruction::BitCast;
2507 return 0;
2508 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002509 case 12: {
2510 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2511 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2512 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2513 return Instruction::AddrSpaceCast;
2514 return Instruction::BitCast;
2515 }
2516 case 13:
2517 // FIXME: this state can be merged with (1), but the following assert
2518 // is useful to check the correcteness of the sequence due to semantic
2519 // change of bitcast.
2520 assert(
2521 SrcTy->isPtrOrPtrVectorTy() &&
2522 MidTy->isPtrOrPtrVectorTy() &&
2523 DstTy->isPtrOrPtrVectorTy() &&
2524 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2525 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2526 "Illegal addrspacecast, bitcast sequence!");
2527 // Allowed, use first cast's opcode
2528 return firstOp;
2529 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002530 // bitcast, addrspacecast -> addrspacecast if the element type of
2531 // bitcast's source is the same as that of addrspacecast's destination.
2532 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2533 return Instruction::AddrSpaceCast;
2534 return 0;
2535
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002536 case 15:
2537 // FIXME: this state can be merged with (1), but the following assert
2538 // is useful to check the correcteness of the sequence due to semantic
2539 // change of bitcast.
2540 assert(
2541 SrcTy->isIntOrIntVectorTy() &&
2542 MidTy->isPtrOrPtrVectorTy() &&
2543 DstTy->isPtrOrPtrVectorTy() &&
2544 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2545 "Illegal inttoptr, bitcast sequence!");
2546 // Allowed, use first cast's opcode
2547 return firstOp;
2548 case 16:
2549 // FIXME: this state can be merged with (2), but the following assert
2550 // is useful to check the correcteness of the sequence due to semantic
2551 // change of bitcast.
2552 assert(
2553 SrcTy->isPtrOrPtrVectorTy() &&
2554 MidTy->isPtrOrPtrVectorTy() &&
2555 DstTy->isIntOrIntVectorTy() &&
2556 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2557 "Illegal bitcast, ptrtoint sequence!");
2558 // Allowed, use second cast's opcode
2559 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002560 case 17:
2561 // (sitofp (zext x)) -> (uitofp x)
2562 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002564 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002566 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567 default:
Craig Topperc514b542012-02-05 22:14:15 +00002568 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
Chris Lattner229907c2011-07-18 04:54:35 +00002572CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002574 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575 // Construct and return the appropriate CastInst subclass
2576 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002577 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2578 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2579 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2580 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2581 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2582 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2583 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2584 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2585 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2586 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2587 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2588 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2589 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2590 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592}
2593
Chris Lattner229907c2011-07-18 04:54:35 +00002594CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002596 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597 // Construct and return the appropriate CastInst subclass
2598 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002599 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2600 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2601 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2602 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2603 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2604 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2605 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2606 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2607 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2608 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2609 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2610 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2611 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2612 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614}
2615
Chris Lattner229907c2011-07-18 04:54:35 +00002616CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002617 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002618 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002619 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002620 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2621 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002622}
2623
Chris Lattner229907c2011-07-18 04:54:35 +00002624CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002625 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002626 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002627 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002628 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2629 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002630}
2631
Chris Lattner229907c2011-07-18 04:54:35 +00002632CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002634 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002635 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002636 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2637 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002638}
2639
Chris Lattner229907c2011-07-18 04:54:35 +00002640CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002641 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002642 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002643 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002644 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2645 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002646}
2647
Chris Lattner229907c2011-07-18 04:54:35 +00002648CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002649 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002650 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002651 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002652 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2653 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002654}
2655
Chris Lattner229907c2011-07-18 04:54:35 +00002656CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002657 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002658 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002659 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002660 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2661 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002662}
2663
Chris Lattner229907c2011-07-18 04:54:35 +00002664CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002665 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002666 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002667 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2668 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2669 "Invalid cast");
2670 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002671 assert((!Ty->isVectorTy() ||
2672 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002673 "Invalid cast");
2674
Matt Arsenault065ced92013-07-31 00:17:33 +00002675 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002676 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002677
Matt Arsenault740980e2014-07-14 17:24:35 +00002678 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002679}
2680
2681/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002682CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2683 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002684 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002685 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2686 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002687 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002688 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002689 assert((!Ty->isVectorTy() ||
2690 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002691 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002692
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002693 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002694 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002695
Matt Arsenault740980e2014-07-14 17:24:35 +00002696 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2697}
2698
2699CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2700 Value *S, Type *Ty,
2701 const Twine &Name,
2702 BasicBlock *InsertAtEnd) {
2703 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2704 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2705
2706 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2707 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2708
2709 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2710}
2711
2712CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2713 Value *S, Type *Ty,
2714 const Twine &Name,
2715 Instruction *InsertBefore) {
2716 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2717 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2718
2719 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002720 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2721
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002722 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002723}
2724
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002725CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2726 const Twine &Name,
2727 Instruction *InsertBefore) {
2728 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2729 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2730 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2731 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2732
2733 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2734}
2735
Matt Arsenault740980e2014-07-14 17:24:35 +00002736CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002737 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002738 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002739 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002740 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002741 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2742 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002743 Instruction::CastOps opcode =
2744 (SrcBits == DstBits ? Instruction::BitCast :
2745 (SrcBits > DstBits ? Instruction::Trunc :
2746 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002747 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002748}
2749
Chris Lattner229907c2011-07-18 04:54:35 +00002750CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002751 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002752 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002753 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002754 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002755 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2756 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002757 Instruction::CastOps opcode =
2758 (SrcBits == DstBits ? Instruction::BitCast :
2759 (SrcBits > DstBits ? Instruction::Trunc :
2760 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002761 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002762}
2763
Chris Lattner229907c2011-07-18 04:54:35 +00002764CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002765 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002766 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002767 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002768 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002769 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2770 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002771 Instruction::CastOps opcode =
2772 (SrcBits == DstBits ? Instruction::BitCast :
2773 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002774 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002775}
2776
Chris Lattner229907c2011-07-18 04:54:35 +00002777CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002778 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002779 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002780 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002781 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002782 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2783 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002784 Instruction::CastOps opcode =
2785 (SrcBits == DstBits ? Instruction::BitCast :
2786 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002787 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002788}
2789
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002790// Check whether it is valid to call getCastOpcode for these types.
2791// This routine must be kept in sync with getCastOpcode.
2792bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2793 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2794 return false;
2795
2796 if (SrcTy == DestTy)
2797 return true;
2798
2799 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2800 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2801 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2802 // An element by element cast. Valid if casting the elements is valid.
2803 SrcTy = SrcVecTy->getElementType();
2804 DestTy = DestVecTy->getElementType();
2805 }
2806
2807 // Get the bit sizes, we'll need these
2808 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2809 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2810
2811 // Run through the possibilities ...
2812 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002813 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002814 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002815 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002816 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002817 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002818 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002819 // Casting from something else
2820 return SrcTy->isPointerTy();
2821 }
2822 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2823 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002824 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002825 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002826 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002827 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002828 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002829 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002830 return false;
2831 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002832 if (DestTy->isVectorTy()) // Casting to vector
2833 return DestBits == SrcBits;
2834 if (DestTy->isPointerTy()) { // Casting to pointer
2835 if (SrcTy->isPointerTy()) // Casting from pointer
2836 return true;
2837 return SrcTy->isIntegerTy(); // Casting from integral
2838 }
2839 if (DestTy->isX86_MMXTy()) {
2840 if (SrcTy->isVectorTy())
2841 return DestBits == SrcBits; // 64-bit vector to MMX
2842 return false;
2843 } // Casting to something else
2844 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002845}
2846
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002847bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2848 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2849 return false;
2850
2851 if (SrcTy == DestTy)
2852 return true;
2853
2854 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2855 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2856 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2857 // An element by element cast. Valid if casting the elements is valid.
2858 SrcTy = SrcVecTy->getElementType();
2859 DestTy = DestVecTy->getElementType();
2860 }
2861 }
2862 }
2863
2864 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2865 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2866 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2867 }
2868 }
2869
2870 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2871 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2872
2873 // Could still have vectors of pointers if the number of elements doesn't
2874 // match
2875 if (SrcBits == 0 || DestBits == 0)
2876 return false;
2877
2878 if (SrcBits != DestBits)
2879 return false;
2880
2881 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2882 return false;
2883
2884 return true;
2885}
2886
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002887bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002888 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002889 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2890 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002891 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002892 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2893 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002894 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002895
2896 return isBitCastable(SrcTy, DestTy);
2897}
2898
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002899// Provide a way to get a "cast" where the cast opcode is inferred from the
2900// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002901// logic in the castIsValid function below. This axiom should hold:
2902// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2903// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002905// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002906Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002907CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002908 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2909 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910
Duncan Sands55e50902008-01-06 10:12:28 +00002911 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2912 "Only first class types are castable!");
2913
Duncan Sandsa8514532011-05-18 07:13:41 +00002914 if (SrcTy == DestTy)
2915 return BitCast;
2916
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002917 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002918 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2919 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002920 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2921 // An element by element cast. Find the appropriate opcode based on the
2922 // element types.
2923 SrcTy = SrcVecTy->getElementType();
2924 DestTy = DestVecTy->getElementType();
2925 }
2926
2927 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002928 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2929 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002930
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002931 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002932 if (DestTy->isIntegerTy()) { // Casting to integral
2933 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002934 if (DestBits < SrcBits)
2935 return Trunc; // int -> smaller int
2936 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002937 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938 return SExt; // signed -> SEXT
2939 else
2940 return ZExt; // unsigned -> ZEXT
2941 } else {
2942 return BitCast; // Same size, No-op cast
2943 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002944 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002945 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002946 return FPToSI; // FP -> sint
2947 else
2948 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002949 } else if (SrcTy->isVectorTy()) {
2950 assert(DestBits == SrcBits &&
2951 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002952 return BitCast; // Same size, no-op cast
2953 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002954 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955 "Casting from a value that is not first-class type");
2956 return PtrToInt; // ptr -> int
2957 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002958 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2959 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002960 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002961 return SIToFP; // sint -> FP
2962 else
2963 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002964 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002965 if (DestBits < SrcBits) {
2966 return FPTrunc; // FP -> smaller FP
2967 } else if (DestBits > SrcBits) {
2968 return FPExt; // FP -> larger FP
2969 } else {
2970 return BitCast; // same size, no-op cast
2971 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002972 } else if (SrcTy->isVectorTy()) {
2973 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002974 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002975 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002976 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002977 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002978 } else if (DestTy->isVectorTy()) {
2979 assert(DestBits == SrcBits &&
2980 "Illegal cast to vector (wrong type or size)");
2981 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002982 } else if (DestTy->isPointerTy()) {
2983 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002984 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2985 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002986 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002987 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002988 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002989 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002990 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002991 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002992 if (SrcTy->isVectorTy()) {
2993 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002994 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002995 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002996 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002997 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002998 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999}
3000
3001//===----------------------------------------------------------------------===//
3002// CastInst SubClass Constructors
3003//===----------------------------------------------------------------------===//
3004
3005/// Check that the construction parameters for a CastInst are correct. This
3006/// could be broken out into the separate constructors but it is useful to have
3007/// it in one place and to eliminate the redundant code for getting the sizes
3008/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003009bool
Chris Lattner229907c2011-07-18 04:54:35 +00003010CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011
3012 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003013 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003014
Chris Lattner37bc78a2010-01-26 21:51:43 +00003015 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3016 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017 return false;
3018
3019 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003020 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3021 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003022
Duncan Sands7f646562011-05-18 09:21:57 +00003023 // If these are vector types, get the lengths of the vectors (using zero for
3024 // scalar types means that checking that vector lengths match also checks that
3025 // scalars are not being converted to vectors or vectors to scalars).
3026 unsigned SrcLength = SrcTy->isVectorTy() ?
3027 cast<VectorType>(SrcTy)->getNumElements() : 0;
3028 unsigned DstLength = DstTy->isVectorTy() ?
3029 cast<VectorType>(DstTy)->getNumElements() : 0;
3030
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031 // Switch on the opcode provided
3032 switch (op) {
3033 default: return false; // This is an input error
3034 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003035 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3036 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003037 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003038 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3039 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003040 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003041 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3042 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003043 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003044 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3045 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003046 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003047 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3048 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003050 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003051 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3052 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003054 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003055 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3056 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003058 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003059 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003060 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3061 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3062 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003063 return SrcTy->getScalarType()->isPointerTy() &&
3064 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003065 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003066 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003067 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003068 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3069 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3070 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003071 return SrcTy->getScalarType()->isIntegerTy() &&
3072 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003073 case Instruction::BitCast: {
3074 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3075 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3076
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077 // BitCast implies a no-op cast of type only. No bits change.
3078 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003079 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003080 return false;
3081
Alp Tokerf907b892013-12-05 05:44:44 +00003082 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003083 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003084 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003085 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3086
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003087 // If both are pointers then the address spaces must match.
3088 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3089 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003090
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003091 // A vector of pointers must have the same number of elements.
3092 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3093 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3094 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3095
3096 return false;
3097 }
3098
3099 return true;
3100 }
3101 case Instruction::AddrSpaceCast: {
3102 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3103 if (!SrcPtrTy)
3104 return false;
3105
3106 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3107 if (!DstPtrTy)
3108 return false;
3109
3110 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3111 return false;
3112
3113 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3114 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3115 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3116
3117 return false;
3118 }
3119
3120 return true;
3121 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003122 }
3123}
3124
3125TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003126 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003127) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003128 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129}
3130
3131TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003132 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003133) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003134 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135}
3136
3137ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003138 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003140 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003141}
3142
3143ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003144 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003145) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003147}
3148SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003149 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003151 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003152}
3153
Jeff Cohencc08c832006-12-02 02:22:01 +00003154SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003155 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003157 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003158}
3159
3160FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003161 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003163 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003164}
3165
3166FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003167 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003169 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003170}
3171
3172FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003173 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003175 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003176}
3177
3178FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003179 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003181 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003182}
3183
3184UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003185 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003187 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003188}
3189
3190UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003191 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003193 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003194}
3195
3196SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003197 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003199 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003200}
3201
3202SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003203 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003205 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003206}
3207
3208FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003209 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003212}
3213
3214FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218}
3219
3220FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003221 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003224}
3225
3226FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
3232PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237
3238PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003239 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003241 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242}
3243
3244IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
3250IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003267
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003268AddrSpaceCastInst::AddrSpaceCastInst(
3269 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3270) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3272}
3273
3274AddrSpaceCastInst::AddrSpaceCastInst(
3275 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3276) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3278}
3279
Chris Lattnerf16dc002006-09-17 19:29:56 +00003280//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003281// CmpInst Classes
3282//===----------------------------------------------------------------------===//
3283
Craig Topper3186c012012-09-23 02:12:10 +00003284void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003285
Craig Topper1c3f2832015-12-15 06:11:33 +00003286CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3287 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003288 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003289 OperandTraits<CmpInst>::op_begin(this),
3290 OperandTraits<CmpInst>::operands(this),
3291 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003292 Op<0>() = LHS;
3293 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003294 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003295 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003296}
Gabor Greiff6caff662008-05-10 08:32:32 +00003297
Craig Topper1c3f2832015-12-15 06:11:33 +00003298CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3299 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003300 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003301 OperandTraits<CmpInst>::op_begin(this),
3302 OperandTraits<CmpInst>::operands(this),
3303 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003304 Op<0>() = LHS;
3305 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003306 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003307 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003308}
3309
3310CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003311CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003312 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003313 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003314 if (InsertBefore)
3315 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3316 S1, S2, Name);
3317 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003318 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003320 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321
3322 if (InsertBefore)
3323 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3324 S1, S2, Name);
3325 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003326 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003327 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003328}
3329
3330CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003331CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003332 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003333 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3335 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003336 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3338 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003339}
3340
3341void CmpInst::swapOperands() {
3342 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3343 IC->swapOperands();
3344 else
3345 cast<FCmpInst>(this)->swapOperands();
3346}
3347
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003348bool CmpInst::isCommutative() const {
3349 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003350 return IC->isCommutative();
3351 return cast<FCmpInst>(this)->isCommutative();
3352}
3353
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003354bool CmpInst::isEquality() const {
3355 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003356 return IC->isEquality();
3357 return cast<FCmpInst>(this)->isEquality();
3358}
3359
3360
Dan Gohman4e724382008-05-31 02:47:54 +00003361CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003362 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003363 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003364 case ICMP_EQ: return ICMP_NE;
3365 case ICMP_NE: return ICMP_EQ;
3366 case ICMP_UGT: return ICMP_ULE;
3367 case ICMP_ULT: return ICMP_UGE;
3368 case ICMP_UGE: return ICMP_ULT;
3369 case ICMP_ULE: return ICMP_UGT;
3370 case ICMP_SGT: return ICMP_SLE;
3371 case ICMP_SLT: return ICMP_SGE;
3372 case ICMP_SGE: return ICMP_SLT;
3373 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003374
Dan Gohman4e724382008-05-31 02:47:54 +00003375 case FCMP_OEQ: return FCMP_UNE;
3376 case FCMP_ONE: return FCMP_UEQ;
3377 case FCMP_OGT: return FCMP_ULE;
3378 case FCMP_OLT: return FCMP_UGE;
3379 case FCMP_OGE: return FCMP_ULT;
3380 case FCMP_OLE: return FCMP_UGT;
3381 case FCMP_UEQ: return FCMP_ONE;
3382 case FCMP_UNE: return FCMP_OEQ;
3383 case FCMP_UGT: return FCMP_OLE;
3384 case FCMP_ULT: return FCMP_OGE;
3385 case FCMP_UGE: return FCMP_OLT;
3386 case FCMP_ULE: return FCMP_OGT;
3387 case FCMP_ORD: return FCMP_UNO;
3388 case FCMP_UNO: return FCMP_ORD;
3389 case FCMP_TRUE: return FCMP_FALSE;
3390 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003391 }
3392}
3393
Pete Cooper1d078692015-12-14 20:29:16 +00003394void ICmpInst::anchor() {}
3395
Reid Spencer266e42b2006-12-23 06:05:41 +00003396ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3397 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003398 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003399 case ICMP_EQ: case ICMP_NE:
3400 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3401 return pred;
3402 case ICMP_UGT: return ICMP_SGT;
3403 case ICMP_ULT: return ICMP_SLT;
3404 case ICMP_UGE: return ICMP_SGE;
3405 case ICMP_ULE: return ICMP_SLE;
3406 }
3407}
3408
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003409ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3410 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003411 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003412 case ICMP_EQ: case ICMP_NE:
3413 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3414 return pred;
3415 case ICMP_SGT: return ICMP_UGT;
3416 case ICMP_SLT: return ICMP_ULT;
3417 case ICMP_SGE: return ICMP_UGE;
3418 case ICMP_SLE: return ICMP_ULE;
3419 }
3420}
3421
Reid Spencer0286bc12007-02-28 22:00:54 +00003422/// Initialize a set of values that all satisfy the condition with C.
3423///
3424ConstantRange
3425ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3426 APInt Lower(C);
3427 APInt Upper(C);
3428 uint32_t BitWidth = C.getBitWidth();
3429 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003430 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003431 case ICmpInst::ICMP_EQ: ++Upper; break;
3432 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003433 case ICmpInst::ICMP_ULT:
3434 Lower = APInt::getMinValue(BitWidth);
3435 // Check for an empty-set condition.
3436 if (Lower == Upper)
3437 return ConstantRange(BitWidth, /*isFullSet=*/false);
3438 break;
3439 case ICmpInst::ICMP_SLT:
3440 Lower = APInt::getSignedMinValue(BitWidth);
3441 // Check for an empty-set condition.
3442 if (Lower == Upper)
3443 return ConstantRange(BitWidth, /*isFullSet=*/false);
3444 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003445 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003446 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003447 // Check for an empty-set condition.
3448 if (Lower == Upper)
3449 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003450 break;
3451 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003452 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003453 // Check for an empty-set condition.
3454 if (Lower == Upper)
3455 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003456 break;
3457 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003458 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003459 // Check for a full-set condition.
3460 if (Lower == Upper)
3461 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003462 break;
3463 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003464 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003465 // Check for a full-set condition.
3466 if (Lower == Upper)
3467 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003468 break;
3469 case ICmpInst::ICMP_UGE:
3470 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003471 // Check for a full-set condition.
3472 if (Lower == Upper)
3473 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003474 break;
3475 case ICmpInst::ICMP_SGE:
3476 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003477 // Check for a full-set condition.
3478 if (Lower == Upper)
3479 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003480 break;
3481 }
3482 return ConstantRange(Lower, Upper);
3483}
3484
Dan Gohman4e724382008-05-31 02:47:54 +00003485CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003486 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003487 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003488 case ICMP_EQ: case ICMP_NE:
3489 return pred;
3490 case ICMP_SGT: return ICMP_SLT;
3491 case ICMP_SLT: return ICMP_SGT;
3492 case ICMP_SGE: return ICMP_SLE;
3493 case ICMP_SLE: return ICMP_SGE;
3494 case ICMP_UGT: return ICMP_ULT;
3495 case ICMP_ULT: return ICMP_UGT;
3496 case ICMP_UGE: return ICMP_ULE;
3497 case ICMP_ULE: return ICMP_UGE;
3498
Reid Spencerd9436b62006-11-20 01:22:35 +00003499 case FCMP_FALSE: case FCMP_TRUE:
3500 case FCMP_OEQ: case FCMP_ONE:
3501 case FCMP_UEQ: case FCMP_UNE:
3502 case FCMP_ORD: case FCMP_UNO:
3503 return pred;
3504 case FCMP_OGT: return FCMP_OLT;
3505 case FCMP_OLT: return FCMP_OGT;
3506 case FCMP_OGE: return FCMP_OLE;
3507 case FCMP_OLE: return FCMP_OGE;
3508 case FCMP_UGT: return FCMP_ULT;
3509 case FCMP_ULT: return FCMP_UGT;
3510 case FCMP_UGE: return FCMP_ULE;
3511 case FCMP_ULE: return FCMP_UGE;
3512 }
3513}
3514
Sanjoy Das6e78b172015-10-22 19:57:34 +00003515CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3516 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3517
3518 switch (pred) {
3519 default:
3520 llvm_unreachable("Unknown predicate!");
3521 case CmpInst::ICMP_ULT:
3522 return CmpInst::ICMP_SLT;
3523 case CmpInst::ICMP_ULE:
3524 return CmpInst::ICMP_SLE;
3525 case CmpInst::ICMP_UGT:
3526 return CmpInst::ICMP_SGT;
3527 case CmpInst::ICMP_UGE:
3528 return CmpInst::ICMP_SGE;
3529 }
3530}
3531
Craig Topper1c3f2832015-12-15 06:11:33 +00003532bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003533 switch (predicate) {
3534 default: return false;
3535 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3536 case ICmpInst::ICMP_UGE: return true;
3537 }
3538}
3539
Craig Topper1c3f2832015-12-15 06:11:33 +00003540bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003541 switch (predicate) {
3542 default: return false;
3543 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3544 case ICmpInst::ICMP_SGE: return true;
3545 }
3546}
3547
Craig Topper1c3f2832015-12-15 06:11:33 +00003548bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003549 switch (predicate) {
3550 default: return false;
3551 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3552 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3553 case FCmpInst::FCMP_ORD: return true;
3554 }
3555}
3556
Craig Topper1c3f2832015-12-15 06:11:33 +00003557bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003558 switch (predicate) {
3559 default: return false;
3560 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3561 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3562 case FCmpInst::FCMP_UNO: return true;
3563 }
3564}
3565
Craig Topper1c3f2832015-12-15 06:11:33 +00003566bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003567 switch(predicate) {
3568 default: return false;
3569 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3570 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3571 }
3572}
3573
Craig Topper1c3f2832015-12-15 06:11:33 +00003574bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003575 switch(predicate) {
3576 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3577 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3578 default: return false;
3579 }
3580}
3581
3582
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003583//===----------------------------------------------------------------------===//
3584// SwitchInst Implementation
3585//===----------------------------------------------------------------------===//
3586
Chris Lattnerbaf00152010-11-17 05:41:46 +00003587void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3588 assert(Value && Default && NumReserved);
3589 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003590 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003591 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003592
Pete Coopereb31b682015-05-21 22:48:54 +00003593 Op<0>() = Value;
3594 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003595}
3596
Chris Lattner2195fc42007-02-24 00:55:48 +00003597/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3598/// switch on and a default destination. The number of additional cases can
3599/// be specified here to make memory allocation more efficient. This
3600/// constructor can also autoinsert before another instruction.
3601SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3602 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003603 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003604 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003605 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003606}
3607
3608/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3609/// switch on and a default destination. The number of additional cases can
3610/// be specified here to make memory allocation more efficient. This
3611/// constructor also autoinserts at the end of the specified BasicBlock.
3612SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3613 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003614 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003615 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003616 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003617}
3618
Misha Brukmanb1c93172005-04-21 23:48:37 +00003619SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003620 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003621 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003622 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003623 Use *OL = getOperandList();
3624 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003625 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003626 OL[i] = InOL[i];
3627 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003628 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003629 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003630}
3631
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003632
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003633/// addCase - Add an entry to the switch instruction...
3634///
Chris Lattner47ac1872005-02-24 05:32:09 +00003635void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003636 unsigned NewCaseIdx = getNumCases();
3637 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003638 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003639 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003640 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003641 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003642 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003643 CaseIt Case(this, NewCaseIdx);
3644 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003645 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003646}
3647
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003648/// removeCase - This method removes the specified case and its successor
3649/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003650void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003651 unsigned idx = i.getCaseIndex();
3652
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003653 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003654
3655 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003656 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003657
Jay Foad14277722011-02-01 09:22:34 +00003658 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003659 if (2 + (idx + 1) * 2 != NumOps) {
3660 OL[2 + idx * 2] = OL[NumOps - 2];
3661 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003662 }
3663
3664 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003665 OL[NumOps-2].set(nullptr);
3666 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003667 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003668}
3669
Jay Foade98f29d2011-04-01 08:00:58 +00003670/// growOperands - grow operands - This grows the operand list in response
3671/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003672///
Jay Foade98f29d2011-04-01 08:00:58 +00003673void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003674 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003675 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003676
3677 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003678 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003679}
3680
3681
3682BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3683 return getSuccessor(idx);
3684}
3685unsigned SwitchInst::getNumSuccessorsV() const {
3686 return getNumSuccessors();
3687}
3688void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3689 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003690}
Chris Lattnerf22be932004-10-15 23:52:53 +00003691
Chris Lattner3ed871f2009-10-27 19:13:16 +00003692//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003693// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003694//===----------------------------------------------------------------------===//
3695
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003696void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003697 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003698 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003699 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003700 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003701 allocHungoffUses(ReservedSpace);
3702
Pete Coopereb31b682015-05-21 22:48:54 +00003703 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003704}
3705
3706
Jay Foade98f29d2011-04-01 08:00:58 +00003707/// growOperands - grow operands - This grows the operand list in response
3708/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003709///
Jay Foade98f29d2011-04-01 08:00:58 +00003710void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003712 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003713
3714 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003715 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003716}
3717
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003718IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3719 Instruction *InsertBefore)
3720: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003721 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003722 init(Address, NumCases);
3723}
3724
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003725IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3726 BasicBlock *InsertAtEnd)
3727: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003728 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003729 init(Address, NumCases);
3730}
3731
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003732IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003733 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3734 nullptr, IBI.getNumOperands()) {
3735 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003736 Use *OL = getOperandList();
3737 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003738 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3739 OL[i] = InOL[i];
3740 SubclassOptionalData = IBI.SubclassOptionalData;
3741}
3742
Chris Lattner3ed871f2009-10-27 19:13:16 +00003743/// addDestination - Add a destination.
3744///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003745void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003746 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003747 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003748 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003749 // Initialize some new operands.
3750 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003751 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003752 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003753}
3754
3755/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003756/// indirectbr instruction.
3757void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3759
3760 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003761 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003762
3763 // Replace this value with the last one.
3764 OL[idx+1] = OL[NumOps-1];
3765
3766 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003767 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003768 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003769}
3770
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003771BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772 return getSuccessor(idx);
3773}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003774unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003775 return getNumSuccessors();
3776}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003777void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003778 setSuccessor(idx, B);
3779}
3780
3781//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003782// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003783//===----------------------------------------------------------------------===//
3784
Chris Lattnerf22be932004-10-15 23:52:53 +00003785// Define these methods here so vtables don't get emitted into every translation
3786// unit that uses these classes.
3787
Pete Cooper75403d72015-06-24 20:22:23 +00003788GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003789 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003790}
3791
Pete Cooper75403d72015-06-24 20:22:23 +00003792BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003793 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003794}
3795
Pete Cooper75403d72015-06-24 20:22:23 +00003796FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003797 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003798}
3799
Pete Cooper75403d72015-06-24 20:22:23 +00003800ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003801 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003802}
3803
Pete Cooper75403d72015-06-24 20:22:23 +00003804ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003805 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003806}
3807
Pete Cooper75403d72015-06-24 20:22:23 +00003808InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003809 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003810}
3811
Pete Cooper75403d72015-06-24 20:22:23 +00003812AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003813 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3814 (Value *)getOperand(0), getAlignment());
3815 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003816 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003817 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003818}
3819
Pete Cooper75403d72015-06-24 20:22:23 +00003820LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003821 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3822 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003823}
3824
Pete Cooper75403d72015-06-24 20:22:23 +00003825StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003826 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003827 getAlignment(), getOrdering(), getSynchScope());
3828
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003829}
3830
Pete Cooper75403d72015-06-24 20:22:23 +00003831AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003832 AtomicCmpXchgInst *Result =
3833 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003834 getSuccessOrdering(), getFailureOrdering(),
3835 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003836 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003837 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003838 return Result;
3839}
3840
Pete Cooper75403d72015-06-24 20:22:23 +00003841AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003842 AtomicRMWInst *Result =
3843 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3844 getOrdering(), getSynchScope());
3845 Result->setVolatile(isVolatile());
3846 return Result;
3847}
3848
Pete Cooper75403d72015-06-24 20:22:23 +00003849FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003850 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3851}
3852
Pete Cooper75403d72015-06-24 20:22:23 +00003853TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003854 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003855}
3856
Pete Cooper75403d72015-06-24 20:22:23 +00003857ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003858 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003859}
3860
Pete Cooper75403d72015-06-24 20:22:23 +00003861SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003862 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003863}
3864
Pete Cooper75403d72015-06-24 20:22:23 +00003865FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003866 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003867}
3868
Pete Cooper75403d72015-06-24 20:22:23 +00003869FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003870 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003871}
3872
Pete Cooper75403d72015-06-24 20:22:23 +00003873UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003874 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003875}
3876
Pete Cooper75403d72015-06-24 20:22:23 +00003877SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003878 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003879}
3880
Pete Cooper75403d72015-06-24 20:22:23 +00003881FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003882 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003883}
3884
Pete Cooper75403d72015-06-24 20:22:23 +00003885FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003886 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003887}
3888
Pete Cooper75403d72015-06-24 20:22:23 +00003889PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003890 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003891}
3892
Pete Cooper75403d72015-06-24 20:22:23 +00003893IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003894 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003895}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003896
Pete Cooper75403d72015-06-24 20:22:23 +00003897BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003898 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003899}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003900
Pete Cooper75403d72015-06-24 20:22:23 +00003901AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003902 return new AddrSpaceCastInst(getOperand(0), getType());
3903}
3904
Pete Cooper75403d72015-06-24 20:22:23 +00003905CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003906 if (hasOperandBundles()) {
3907 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3908 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3909 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003910 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003911}
3912
Pete Cooper75403d72015-06-24 20:22:23 +00003913SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003914 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003915}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003916
Pete Cooper75403d72015-06-24 20:22:23 +00003917VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003918 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003919}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003920
Pete Cooper75403d72015-06-24 20:22:23 +00003921ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003922 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003923}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003924
Pete Cooper75403d72015-06-24 20:22:23 +00003925InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003926 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003927}
3928
Pete Cooper75403d72015-06-24 20:22:23 +00003929ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003930 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003931}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003932
Pete Cooper75403d72015-06-24 20:22:23 +00003933PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003934
Pete Cooper75403d72015-06-24 20:22:23 +00003935LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003936 return new LandingPadInst(*this);
3937}
3938
Pete Cooper75403d72015-06-24 20:22:23 +00003939ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003940 return new(getNumOperands()) ReturnInst(*this);
3941}
3942
Pete Cooper75403d72015-06-24 20:22:23 +00003943BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003944 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003945}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003946
Pete Cooper75403d72015-06-24 20:22:23 +00003947SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003948
Pete Cooper75403d72015-06-24 20:22:23 +00003949IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003950 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003954 if (hasOperandBundles()) {
3955 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3956 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3957 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003958 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003959}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003960
Pete Cooper75403d72015-06-24 20:22:23 +00003961ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003962
David Majnemer654e1302015-07-31 17:58:14 +00003963CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3964 return new (getNumOperands()) CleanupReturnInst(*this);
3965}
3966
David Majnemer654e1302015-07-31 17:58:14 +00003967CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003968 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003969}
3970
David Majnemer8a1c45d2015-12-12 05:38:55 +00003971CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3972 return new CatchSwitchInst(*this);
3973}
3974
3975FuncletPadInst *FuncletPadInst::cloneImpl() const {
3976 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003977}
3978
Pete Cooper75403d72015-06-24 20:22:23 +00003979UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003980 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003981 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003982}