blob: 126a96635ee8c106c9ada7313d5ed21b7898f9a3 [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000017#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
Bjorn Pettersson550517b2018-06-26 06:17:00 +000048// AllocaInst Class
49//===----------------------------------------------------------------------===//
50
51Optional<uint64_t>
52AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
53 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
54 if (isArrayAllocation()) {
55 auto C = dyn_cast<ConstantInt>(getArraySize());
56 if (!C)
57 return None;
58 Size *= C->getZExtValue();
59 }
60 return Size;
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000064// CallSite Class
65//===----------------------------------------------------------------------===//
66
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000067User::op_iterator CallSite::getCallee() const {
68 Instruction *II(getInstruction());
69 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000070 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000071 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000072}
73
Gordon Henriksen14a55692007-12-10 02:14:30 +000074//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000075// SelectInst Class
76//===----------------------------------------------------------------------===//
77
78/// areInvalidOperands - Return a string if the specified operands are invalid
79/// for a select operation, otherwise return null.
80const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
81 if (Op1->getType() != Op2->getType())
82 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000083
84 if (Op1->getType()->isTokenTy())
85 return "select values cannot have token type";
86
Chris Lattner229907c2011-07-18 04:54:35 +000087 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000088 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000089 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000090 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000091 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000092 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000093 return "selected values for vector select must be vectors";
94 if (ET->getNumElements() != VT->getNumElements())
95 return "vector select requires selected vectors to have "
96 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000097 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000098 return "select condition must be i1 or <n x i1>";
99 }
Craig Topperc6207612014-04-09 06:08:46 +0000100 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000101}
102
Chris Lattner88107952008-12-29 00:12:50 +0000103//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000104// PHINode Class
105//===----------------------------------------------------------------------===//
106
107PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000108 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
109 ReservedSpace(PN.getNumOperands()) {
110 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(PN.op_begin(), PN.op_end(), op_begin());
112 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000113 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000114}
115
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000116// removeIncomingValue - Remove an incoming value. This is useful if a
117// predecessor basic block is deleted.
118Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120
121 // Move everything after this operand down.
122 //
123 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000124 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000126 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
127 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128
129 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000130 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000131 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000132
133 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000134 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000136 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000137 eraseFromParent();
138 }
139 return Removed;
140}
141
Jay Foade98f29d2011-04-01 08:00:58 +0000142/// growOperands - grow operands - This grows the operand list in response
143/// to a push_back style of operation. This grows the number of ops by 1.5
144/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145///
Jay Foade98f29d2011-04-01 08:00:58 +0000146void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000147 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000148 unsigned NumOps = e + e / 2;
149 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
150
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000152 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153}
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155/// hasConstantValue - If the specified PHI node always merges together the same
156/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000157Value *PHINode::hasConstantValue() const {
158 // Exploit the fact that phi nodes always have at least one entry.
159 Value *ConstantValue = getIncomingValue(0);
160 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000161 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
162 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000163 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000164 // The case where the first value is this PHI.
165 ConstantValue = getIncomingValue(i);
166 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000167 if (ConstantValue == this)
168 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000169 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000170}
171
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000172/// hasConstantOrUndefValue - Whether the specified PHI node always merges
173/// together the same value, assuming that undefs result in the same value as
174/// non-undefs.
175/// Unlike \ref hasConstantValue, this does not return a value because the
176/// unique non-undef incoming value need not dominate the PHI node.
177bool PHINode::hasConstantOrUndefValue() const {
178 Value *ConstantValue = nullptr;
179 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
180 Value *Incoming = getIncomingValue(i);
181 if (Incoming != this && !isa<UndefValue>(Incoming)) {
182 if (ConstantValue && ConstantValue != Incoming)
183 return false;
184 ConstantValue = Incoming;
185 }
186 }
187 return true;
188}
189
Bill Wendlingfae14752011-08-12 20:24:12 +0000190//===----------------------------------------------------------------------===//
191// LandingPadInst Implementation
192//===----------------------------------------------------------------------===//
193
David Majnemer7fddecc2015-06-17 20:52:32 +0000194LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
195 const Twine &NameStr, Instruction *InsertBefore)
196 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
197 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000198}
199
David Majnemer7fddecc2015-06-17 20:52:32 +0000200LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
201 const Twine &NameStr, BasicBlock *InsertAtEnd)
202 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
203 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000204}
205
206LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000207 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
208 LP.getNumOperands()),
209 ReservedSpace(LP.getNumOperands()) {
210 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000211 Use *OL = getOperandList();
212 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000213 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
214 OL[I] = InOL[I];
215
216 setCleanup(LP.isCleanup());
217}
218
David Majnemer7fddecc2015-06-17 20:52:32 +0000219LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 const Twine &NameStr,
221 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000222 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000223}
224
David Majnemer7fddecc2015-06-17 20:52:32 +0000225LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000226 const Twine &NameStr,
227 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000228 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000229}
230
David Majnemer7fddecc2015-06-17 20:52:32 +0000231void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000232 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000233 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000234 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000235 setName(NameStr);
236 setCleanup(false);
237}
238
239/// growOperands - grow operands - This grows the operand list in response to a
240/// push_back style of operation. This grows the number of ops by 2 times.
241void LandingPadInst::growOperands(unsigned Size) {
242 unsigned e = getNumOperands();
243 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000244 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000245 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000246}
247
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000248void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000249 unsigned OpNo = getNumOperands();
250 growOperands(1);
251 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000252 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000253 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000254}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000255
256//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257// CallInst Implementation
258//===----------------------------------------------------------------------===//
259
David Blaikie348de692015-04-23 21:36:23 +0000260void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000261 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000262 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000263 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
264 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000265 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266
Jay Foad5bd375a2011-07-15 08:37:34 +0000267#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000268 assert((Args.size() == FTy->getNumParams() ||
269 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000270 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000271
272 for (unsigned i = 0; i != Args.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000273 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000274 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000275 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000276#endif
277
278 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000279
280 auto It = populateBundleOperandInfos(Bundles, Args.size());
281 (void)It;
282 assert(It + 1 == op_end() && "Should add up!");
283
Jay Foad5bd375a2011-07-15 08:37:34 +0000284 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Jay Foad5bd375a2011-07-15 08:37:34 +0000287void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000288 FTy =
289 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000290 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000291 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000292
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000293 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000294
295 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000298CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
299 : CallBase<CallInst>(
300 cast<FunctionType>(
301 cast<PointerType>(Func->getType())->getElementType())
302 ->getReturnType(),
303 Instruction::Call,
304 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1,
305 InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000306 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000309CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
310 : CallBase<CallInst>(
311 cast<FunctionType>(
312 cast<PointerType>(Func->getType())->getElementType())
313 ->getReturnType(),
314 Instruction::Call,
315 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000316 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
Misha Brukmanb1c93172005-04-21 23:48:37 +0000319CallInst::CallInst(const CallInst &CI)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000320 : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
321 OperandTraits<CallBase<CallInst>>::op_end(this) -
322 CI.getNumOperands(),
323 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000324 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000325 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000326
Jay Foad5bd375a2011-07-15 08:37:34 +0000327 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000328 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
329 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000330 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Sanjoy Das2d161452015-11-18 06:23:38 +0000333CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
334 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000335 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000336
337 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
338 InsertPt);
339 NewCI->setTailCallKind(CI->getTailCallKind());
340 NewCI->setCallingConv(CI->getCallingConv());
341 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000342 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000343 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000344 return NewCI;
345}
346
Hal Finkele87ad542016-07-10 23:01:32 +0000347
Reid Klecknera0b45f42017-05-03 18:17:31 +0000348
Hal Finkele87ad542016-07-10 23:01:32 +0000349
Eric Christopher901b1a72008-05-16 20:39:43 +0000350
Amaury Secheta65a2372016-06-15 05:14:29 +0000351
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000352
Reid Klecknera0b45f42017-05-03 18:17:31 +0000353
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000354
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000355
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000356/// IsConstantOne - Return true only if val is constant int 1
357static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000358 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000359 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
360 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000361}
362
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000363static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000364 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000365 Type *AllocTy, Value *AllocSize,
366 Value *ArraySize,
367 ArrayRef<OperandBundleDef> OpB,
368 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000369 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000370 "createMalloc needs either InsertBefore or InsertAtEnd");
371
Fangrui Songf78650a2018-07-30 19:41:25 +0000372 // malloc(type) becomes:
Victor Hernandez788eaab2009-09-18 19:20:02 +0000373 // bitcast (i8* malloc(typeSize)) to type*
374 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000375 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000376 if (!ArraySize)
377 ArraySize = ConstantInt::get(IntPtrTy, 1);
378 else if (ArraySize->getType() != IntPtrTy) {
379 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000380 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
381 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000382 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000383 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
384 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000385 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000386
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000387 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000388 if (IsConstantOne(AllocSize)) {
389 AllocSize = ArraySize; // Operand * 1 = Operand
390 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
391 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
392 false /*ZExt*/);
393 // Malloc arg is constant product of type size and array size
394 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
395 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000396 // Multiply type size by the array size...
397 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000398 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
399 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000400 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000401 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
402 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000403 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000404 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000405
Victor Hernandez788eaab2009-09-18 19:20:02 +0000406 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000408 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
409 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000410 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000411 Value *MallocFunc = MallocF;
412 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000413 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000414 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000415 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000416 CallInst *MCall = nullptr;
417 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000419 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
420 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000421 Result = MCall;
422 if (Result->getType() != AllocPtrType)
423 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000426 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000427 Result = MCall;
428 if (Result->getType() != AllocPtrType) {
429 InsertAtEnd->getInstList().push_back(MCall);
430 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000431 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000432 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000433 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000434 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000435 if (Function *F = dyn_cast<Function>(MallocFunc)) {
436 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000437 if (!F->returnDoesNotAlias())
438 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000440 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000441
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443}
444
445/// CreateMalloc - Generate the IR for a call to malloc:
446/// 1. Compute the malloc call's argument as the specified type's size,
447/// possibly multiplied by the array size if the array size is not
448/// constant 1.
449/// 2. Call malloc with that argument.
450/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000451Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000452 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000453 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000454 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000455 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000456 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000457 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000458}
David Majnemerfadc6db2016-04-29 08:07:22 +0000459Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
460 Type *IntPtrTy, Type *AllocTy,
461 Value *AllocSize, Value *ArraySize,
462 ArrayRef<OperandBundleDef> OpB,
463 Function *MallocF,
464 const Twine &Name) {
465 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
466 ArraySize, OpB, MallocF, Name);
467}
468
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000469/// CreateMalloc - Generate the IR for a call to malloc:
470/// 1. Compute the malloc call's argument as the specified type's size,
471/// possibly multiplied by the array size if the array size is not
472/// constant 1.
473/// 2. Call malloc with that argument.
474/// 3. Bitcast the result of the malloc call to the specified type.
475/// Note: This function does not add the bitcast to the basic block, that is the
476/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000477Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000478 Type *IntPtrTy, Type *AllocTy,
Fangrui Songf78650a2018-07-30 19:41:25 +0000479 Value *AllocSize, Value *ArraySize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000480 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000481 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000482 ArraySize, None, MallocF, Name);
483}
484Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
485 Type *IntPtrTy, Type *AllocTy,
486 Value *AllocSize, Value *ArraySize,
487 ArrayRef<OperandBundleDef> OpB,
488 Function *MallocF, const Twine &Name) {
489 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
490 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000492
David Majnemerfadc6db2016-04-29 08:07:22 +0000493static Instruction *createFree(Value *Source,
494 ArrayRef<OperandBundleDef> Bundles,
495 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*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000508 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
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);
David Majnemerfadc6db2016-04-29 08:07:22 +0000514 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000515 } else {
516 if (Source->getType() != IntPtrTy)
517 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000518 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000519 }
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) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000529 return createFree(Source, None, InsertBefore, nullptr);
530}
531Instruction *CallInst::CreateFree(Value *Source,
532 ArrayRef<OperandBundleDef> Bundles,
533 Instruction *InsertBefore) {
534 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000535}
536
537/// CreateFree - Generate the IR for a call to the builtin free function.
538/// Note: This function does not add the call to the basic block, that is the
539/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000540Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000541 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
542 assert(FreeCall && "CreateFree did not create a CallInst");
543 return FreeCall;
544}
545Instruction *CallInst::CreateFree(Value *Source,
546 ArrayRef<OperandBundleDef> Bundles,
547 BasicBlock *InsertAtEnd) {
548 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000549 assert(FreeCall && "CreateFree did not create a CallInst");
550 return FreeCall;
551}
552
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553//===----------------------------------------------------------------------===//
554// InvokeInst Implementation
555//===----------------------------------------------------------------------===//
556
David Blaikie3e807092015-05-13 18:35:26 +0000557void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
558 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000559 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000560 const Twine &NameStr) {
561 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000562
Sanjoy Das9303c242015-09-24 19:14:18 +0000563 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
564 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000565 Op<-3>() = Fn;
566 Op<-2>() = IfNormal;
567 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000568
569#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000570 assert(((Args.size() == FTy->getNumParams()) ||
571 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000572 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573
Jay Foad5bd375a2011-07-15 08:37:34 +0000574 for (unsigned i = 0, e = Args.size(); i != e; i++)
Fangrui Songf78650a2018-07-30 19:41:25 +0000575 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000576 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000577 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000578#endif
579
580 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000581
582 auto It = populateBundleOperandInfos(Bundles, Args.size());
583 (void)It;
584 assert(It + 3 == op_end() && "Should add up!");
585
Jay Foad5bd375a2011-07-15 08:37:34 +0000586 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Misha Brukmanb1c93172005-04-21 23:48:37 +0000589InvokeInst::InvokeInst(const InvokeInst &II)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000590 : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
591 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
592 II.getNumOperands(),
593 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000594 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000595 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000596 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
597 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000598 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599}
600
Sanjoy Das2d161452015-11-18 06:23:38 +0000601InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
602 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000603 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000604
605 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
606 II->getUnwindDest(), Args, OpB,
607 II->getName(), InsertPt);
608 NewII->setCallingConv(II->getCallingConv());
609 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000610 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000611 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000612 return NewII;
613}
614
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000615
Bill Wendlingfae14752011-08-12 20:24:12 +0000616LandingPadInst *InvokeInst::getLandingPadInst() const {
617 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
618}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000619
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000620//===----------------------------------------------------------------------===//
621// ReturnInst Implementation
622//===----------------------------------------------------------------------===//
623
Chris Lattner2195fc42007-02-24 00:55:48 +0000624ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000625 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000626 OperandTraits<ReturnInst>::op_end(this) -
627 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000628 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000629 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000630 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000631 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000632}
633
Owen Anderson55f1c092009-08-13 21:58:54 +0000634ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
635 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000636 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
637 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000638 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000639 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000640}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000641
Owen Anderson55f1c092009-08-13 21:58:54 +0000642ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000644 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
645 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000646 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000647 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000648}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000649
Owen Anderson55f1c092009-08-13 21:58:54 +0000650ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000652 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000653}
654
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000656// ResumeInst Implementation
657//===----------------------------------------------------------------------===//
658
659ResumeInst::ResumeInst(const ResumeInst &RI)
660 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
661 OperandTraits<ResumeInst>::op_begin(this), 1) {
662 Op<0>() = RI.Op<0>();
663}
664
665ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
666 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
667 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
668 Op<0>() = Exn;
669}
670
671ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
672 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
673 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
674 Op<0>() = Exn;
675}
676
Bill Wendlingf891bf82011-07-31 06:30:59 +0000677//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000678// CleanupReturnInst Implementation
679//===----------------------------------------------------------------------===//
680
681CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
682 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
683 OperandTraits<CleanupReturnInst>::op_end(this) -
684 CRI.getNumOperands(),
685 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000686 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000687 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000688 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000689 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000690}
691
David Majnemer8a1c45d2015-12-12 05:38:55 +0000692void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000693 if (UnwindBB)
694 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000695
David Majnemer8a1c45d2015-12-12 05:38:55 +0000696 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000697 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000698 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000699}
700
David Majnemer8a1c45d2015-12-12 05:38:55 +0000701CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
702 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000703 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
704 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000705 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
706 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000707 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000708}
709
David Majnemer8a1c45d2015-12-12 05:38:55 +0000710CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
711 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000712 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
713 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000714 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
715 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000716 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000717}
718
David Majnemer654e1302015-07-31 17:58:14 +0000719//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000720// CatchReturnInst Implementation
721//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000722void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000723 Op<0>() = CatchPad;
724 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000725}
David Majnemer654e1302015-07-31 17:58:14 +0000726
727CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
728 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000729 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
730 Op<0>() = CRI.Op<0>();
731 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000732}
733
David Majnemer8a1c45d2015-12-12 05:38:55 +0000734CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000735 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000736 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000737 OperandTraits<CatchReturnInst>::op_begin(this), 2,
738 InsertBefore) {
739 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000740}
741
David Majnemer8a1c45d2015-12-12 05:38:55 +0000742CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000743 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000744 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000745 OperandTraits<CatchReturnInst>::op_begin(this), 2,
746 InsertAtEnd) {
747 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000748}
749
David Majnemer654e1302015-07-31 17:58:14 +0000750//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000751// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000752//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000753
754CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
755 unsigned NumReservedValues,
756 const Twine &NameStr,
757 Instruction *InsertBefore)
758 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
759 InsertBefore) {
760 if (UnwindDest)
761 ++NumReservedValues;
762 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000763 setName(NameStr);
764}
765
David Majnemer8a1c45d2015-12-12 05:38:55 +0000766CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
767 unsigned NumReservedValues,
768 const Twine &NameStr, BasicBlock *InsertAtEnd)
769 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000770 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000771 if (UnwindDest)
772 ++NumReservedValues;
773 init(ParentPad, UnwindDest, NumReservedValues + 1);
774 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000775}
776
David Majnemer8a1c45d2015-12-12 05:38:55 +0000777CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
778 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
779 CSI.getNumOperands()) {
780 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
781 setNumHungOffUseOperands(ReservedSpace);
782 Use *OL = getOperandList();
783 const Use *InOL = CSI.getOperandList();
784 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
785 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000786}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000787
788void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
789 unsigned NumReservedValues) {
790 assert(ParentPad && NumReservedValues);
791
792 ReservedSpace = NumReservedValues;
793 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
794 allocHungoffUses(ReservedSpace);
795
796 Op<0>() = ParentPad;
797 if (UnwindDest) {
798 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
799 setUnwindDest(UnwindDest);
800 }
801}
802
803/// growOperands - grow operands - This grows the operand list in response to a
804/// push_back style of operation. This grows the number of ops by 2 times.
805void CatchSwitchInst::growOperands(unsigned Size) {
806 unsigned NumOperands = getNumOperands();
807 assert(NumOperands >= 1);
808 if (ReservedSpace >= NumOperands + Size)
809 return;
810 ReservedSpace = (NumOperands + Size / 2) * 2;
811 growHungoffUses(ReservedSpace);
812}
813
814void CatchSwitchInst::addHandler(BasicBlock *Handler) {
815 unsigned OpNo = getNumOperands();
816 growOperands(1);
817 assert(OpNo < ReservedSpace && "Growing didn't work!");
818 setNumHungOffUseOperands(getNumOperands() + 1);
819 getOperandList()[OpNo] = Handler;
820}
821
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000822void CatchSwitchInst::removeHandler(handler_iterator HI) {
823 // Move all subsequent handlers up one.
824 Use *EndDst = op_end() - 1;
825 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
826 *CurDst = *(CurDst + 1);
827 // Null out the last handler use.
828 *EndDst = nullptr;
829
830 setNumHungOffUseOperands(getNumOperands() - 1);
831}
832
David Majnemer8a1c45d2015-12-12 05:38:55 +0000833//===----------------------------------------------------------------------===//
834// FuncletPadInst Implementation
835//===----------------------------------------------------------------------===//
836void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
837 const Twine &NameStr) {
838 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
839 std::copy(Args.begin(), Args.end(), op_begin());
840 setParentPad(ParentPad);
841 setName(NameStr);
842}
843
844FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
845 : Instruction(FPI.getType(), FPI.getOpcode(),
846 OperandTraits<FuncletPadInst>::op_end(this) -
847 FPI.getNumOperands(),
848 FPI.getNumOperands()) {
849 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
850 setParentPad(FPI.getParentPad());
851}
852
853FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
854 ArrayRef<Value *> Args, unsigned Values,
855 const Twine &NameStr, Instruction *InsertBefore)
856 : Instruction(ParentPad->getType(), Op,
857 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
858 InsertBefore) {
859 init(ParentPad, Args, NameStr);
860}
861
862FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
863 ArrayRef<Value *> Args, unsigned Values,
864 const Twine &NameStr, BasicBlock *InsertAtEnd)
865 : Instruction(ParentPad->getType(), Op,
866 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
867 InsertAtEnd) {
868 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000869}
870
871//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000872// UnreachableInst Implementation
873//===----------------------------------------------------------------------===//
874
Fangrui Songf78650a2018-07-30 19:41:25 +0000875UnreachableInst::UnreachableInst(LLVMContext &Context,
Owen Anderson55f1c092009-08-13 21:58:54 +0000876 Instruction *InsertBefore)
877 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000878 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000879}
Owen Anderson55f1c092009-08-13 21:58:54 +0000880UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
881 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000882 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000883}
884
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000885//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886// BranchInst Implementation
887//===----------------------------------------------------------------------===//
888
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000889void BranchInst::AssertOK() {
890 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000891 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893}
894
Chris Lattner2195fc42007-02-24 00:55:48 +0000895BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000896 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000897 OperandTraits<BranchInst>::op_end(this) - 1,
898 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000899 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000900 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000901}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000902
Chris Lattner2195fc42007-02-24 00:55:48 +0000903BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
904 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000905 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000906 OperandTraits<BranchInst>::op_end(this) - 3,
907 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000908 Op<-1>() = IfTrue;
909 Op<-2>() = IfFalse;
910 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000911#ifndef NDEBUG
912 AssertOK();
913#endif
914}
915
916BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000917 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000918 OperandTraits<BranchInst>::op_end(this) - 1,
919 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000920 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000921 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000922}
923
924BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
925 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000926 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000927 OperandTraits<BranchInst>::op_end(this) - 3,
928 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000929 Op<-1>() = IfTrue;
930 Op<-2>() = IfFalse;
931 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000932#ifndef NDEBUG
933 AssertOK();
934#endif
935}
936
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000938 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000939 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
940 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000941 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 if (BI.getNumOperands() != 1) {
943 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000944 Op<-3>() = BI.Op<-3>();
945 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000946 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000947 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948}
949
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000950void BranchInst::swapSuccessors() {
951 assert(isConditional() &&
952 "Cannot swap successors of an unconditional branch");
953 Op<-1>().swap(Op<-2>());
954
955 // Update profile metadata if present and it matches our structural
956 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +0000957 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000958}
959
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000960//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000961// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962//===----------------------------------------------------------------------===//
963
Owen Andersonb6b25302009-07-14 23:09:55 +0000964static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000966 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000967 else {
968 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000969 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000970 assert(Amt->getType()->isIntegerTy() &&
971 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000972 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000973 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974}
975
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000976AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000977 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000978 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000979
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000980AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000981 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000982 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +0000983
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000984AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +0000985 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000986 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
987
988AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
989 const Twine &Name, BasicBlock *InsertAtEnd)
990 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
991
992AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
993 unsigned Align, const Twine &Name,
994 Instruction *InsertBefore)
995 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
996 getAISize(Ty->getContext(), ArraySize), InsertBefore),
997 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000998 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000999 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001000 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001}
1002
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001003AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1004 unsigned Align, const Twine &Name,
1005 BasicBlock *InsertAtEnd)
1006 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1007 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001008 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001009 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001010 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001011 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012}
1013
Victor Hernandez8acf2952009-10-23 21:09:37 +00001014void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001015 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001016 assert(Align <= MaximumAlignment &&
1017 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001018 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1019 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001020 assert(getAlignment() == Align && "Alignment representation error!");
1021}
1022
Victor Hernandez8acf2952009-10-23 21:09:37 +00001023bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001024 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001025 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001026 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001027}
1028
Chris Lattner8b291e62008-11-26 02:54:17 +00001029/// isStaticAlloca - Return true if this alloca is in the entry block of the
1030/// function and is a constant size. If so, the code generator will fold it
1031/// into the prolog/epilog code, so it is basically free.
1032bool AllocaInst::isStaticAlloca() const {
1033 // Must be constant size.
1034 if (!isa<ConstantInt>(getArraySize())) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001035
Chris Lattner8b291e62008-11-26 02:54:17 +00001036 // Must be in the entry block.
1037 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001038 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001039}
1040
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001042// LoadInst Implementation
1043//===----------------------------------------------------------------------===//
1044
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001046 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001048 assert(!(isAtomic() && getAlignment() == 0) &&
1049 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050}
1051
Daniel Dunbar4975db62009-07-25 04:41:11 +00001052LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001053 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054
Daniel Dunbar4975db62009-07-25 04:41:11 +00001055LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001056 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057
David Blaikie0c28fd72015-05-20 21:46:30 +00001058LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001060 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001061
1062LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1063 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001064 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001065
David Blaikieb7a029872015-04-17 19:56:21 +00001066LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001067 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001068 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001069 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001071LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001072 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001073 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001074 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001075
David Blaikie15d9a4c2015-04-06 20:59:48 +00001076LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001077 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001078 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001079 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001080 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001081 setVolatile(isVolatile);
1082 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001083 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001084 AssertOK();
1085 setName(Name);
1086}
1087
Fangrui Songf78650a2018-07-30 19:41:25 +00001088LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001089 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001090 SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001091 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001092 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001093 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001094 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001095 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001096 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001097 AssertOK();
1098 setName(Name);
1099}
1100
Daniel Dunbar27096822009-08-11 18:11:15 +00001101LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1102 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1103 Load, Ptr, InsertBef) {
1104 setVolatile(false);
1105 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001106 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001107 AssertOK();
1108 if (Name && Name[0]) setName(Name);
1109}
1110
1111LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1112 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1113 Load, Ptr, InsertAE) {
1114 setVolatile(false);
1115 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001116 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001117 AssertOK();
1118 if (Name && Name[0]) setName(Name);
1119}
1120
David Blaikie0c28fd72015-05-20 21:46:30 +00001121LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001122 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001123 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1124 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001125 setVolatile(isVolatile);
1126 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001127 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001128 AssertOK();
1129 if (Name && Name[0]) setName(Name);
1130}
1131
1132LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1133 BasicBlock *InsertAE)
1134 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1135 Load, Ptr, InsertAE) {
1136 setVolatile(isVolatile);
1137 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001138 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001139 AssertOK();
1140 if (Name && Name[0]) setName(Name);
1141}
1142
Christopher Lamb84485702007-04-22 19:24:39 +00001143void LoadInst::setAlignment(unsigned Align) {
1144 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001145 assert(Align <= MaximumAlignment &&
1146 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001147 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001148 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001149 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001150}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001151
1152//===----------------------------------------------------------------------===//
1153// StoreInst Implementation
1154//===----------------------------------------------------------------------===//
1155
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001156void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001157 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001158 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001159 "Ptr must have pointer type!");
1160 assert(getOperand(0)->getType() ==
1161 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001162 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001163 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001164 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001165}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001166
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001167StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001168 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001169
1170StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001171 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001172
Misha Brukmanb1c93172005-04-21 23:48:37 +00001173StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001174 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001175 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001176
1177StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001178 BasicBlock *InsertAtEnd)
1179 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1180
1181StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1182 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001183 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001184 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001185
1186StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1187 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001188 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001189 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001190
Misha Brukmanb1c93172005-04-21 23:48:37 +00001191StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001192 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001193 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001194 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001195 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001196 OperandTraits<StoreInst>::op_begin(this),
1197 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001198 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001199 Op<0>() = val;
1200 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001201 setVolatile(isVolatile);
1202 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001203 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001204 AssertOK();
1205}
1206
1207StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001208 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001209 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001210 BasicBlock *InsertAtEnd)
1211 : Instruction(Type::getVoidTy(val->getContext()), Store,
1212 OperandTraits<StoreInst>::op_begin(this),
1213 OperandTraits<StoreInst>::operands(this),
1214 InsertAtEnd) {
1215 Op<0>() = val;
1216 Op<1>() = addr;
1217 setVolatile(isVolatile);
1218 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001219 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001220 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001221}
1222
Christopher Lamb84485702007-04-22 19:24:39 +00001223void StoreInst::setAlignment(unsigned Align) {
1224 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001225 assert(Align <= MaximumAlignment &&
1226 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001227 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001228 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001229 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001230}
1231
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001232//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001233// AtomicCmpXchgInst Implementation
1234//===----------------------------------------------------------------------===//
1235
1236void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001237 AtomicOrdering SuccessOrdering,
1238 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001239 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001240 Op<0>() = Ptr;
1241 Op<1>() = Cmp;
1242 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001243 setSuccessOrdering(SuccessOrdering);
1244 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001245 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001246
1247 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1248 "All operands must be non-null!");
1249 assert(getOperand(0)->getType()->isPointerTy() &&
1250 "Ptr must have pointer type!");
1251 assert(getOperand(1)->getType() ==
1252 cast<PointerType>(getOperand(0)->getType())->getElementType()
1253 && "Ptr must be a pointer to Cmp type!");
1254 assert(getOperand(2)->getType() ==
1255 cast<PointerType>(getOperand(0)->getType())->getElementType()
1256 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001257 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001258 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001259 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001260 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001261 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1262 "AtomicCmpXchg failure argument shall be no stronger than the success "
1263 "argument");
1264 assert(FailureOrdering != AtomicOrdering::Release &&
1265 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001266 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001267}
1268
1269AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001270 AtomicOrdering SuccessOrdering,
1271 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001272 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001273 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001274 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001275 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001276 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1277 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001278 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001279}
1280
1281AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001282 AtomicOrdering SuccessOrdering,
1283 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001284 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001285 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001286 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001287 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001288 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1289 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001290 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001291}
Tim Northover420a2162014-06-13 14:24:07 +00001292
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001293//===----------------------------------------------------------------------===//
1294// AtomicRMWInst Implementation
1295//===----------------------------------------------------------------------===//
1296
1297void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1298 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001299 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001300 Op<0>() = Ptr;
1301 Op<1>() = Val;
1302 setOperation(Operation);
1303 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001304 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001305
1306 assert(getOperand(0) && getOperand(1) &&
1307 "All operands must be non-null!");
1308 assert(getOperand(0)->getType()->isPointerTy() &&
1309 "Ptr must have pointer type!");
1310 assert(getOperand(1)->getType() ==
1311 cast<PointerType>(getOperand(0)->getType())->getElementType()
1312 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001313 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001314 "AtomicRMW instructions must be atomic!");
1315}
1316
1317AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1318 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001319 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001320 Instruction *InsertBefore)
1321 : Instruction(Val->getType(), AtomicRMW,
1322 OperandTraits<AtomicRMWInst>::op_begin(this),
1323 OperandTraits<AtomicRMWInst>::operands(this),
1324 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001325 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001326}
1327
1328AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1329 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001330 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001331 BasicBlock *InsertAtEnd)
1332 : Instruction(Val->getType(), AtomicRMW,
1333 OperandTraits<AtomicRMWInst>::op_begin(this),
1334 OperandTraits<AtomicRMWInst>::operands(this),
1335 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001336 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001337}
1338
Matt Arsenaultb02ba992018-10-02 23:44:11 +00001339StringRef AtomicRMWInst::getOperationName(BinOp Op) {
1340 switch (Op) {
1341 case AtomicRMWInst::Xchg:
1342 return "xchg";
1343 case AtomicRMWInst::Add:
1344 return "add";
1345 case AtomicRMWInst::Sub:
1346 return "sub";
1347 case AtomicRMWInst::And:
1348 return "and";
1349 case AtomicRMWInst::Nand:
1350 return "nand";
1351 case AtomicRMWInst::Or:
1352 return "or";
1353 case AtomicRMWInst::Xor:
1354 return "xor";
1355 case AtomicRMWInst::Max:
1356 return "max";
1357 case AtomicRMWInst::Min:
1358 return "min";
1359 case AtomicRMWInst::UMax:
1360 return "umax";
1361 case AtomicRMWInst::UMin:
1362 return "umin";
1363 case AtomicRMWInst::BAD_BINOP:
1364 return "<invalid operation>";
1365 }
1366
1367 llvm_unreachable("invalid atomicrmw operation");
1368}
1369
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001370//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001371// FenceInst Implementation
1372//===----------------------------------------------------------------------===//
1373
Fangrui Songf78650a2018-07-30 19:41:25 +00001374FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001375 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001376 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001377 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001378 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001379 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001380}
1381
Fangrui Songf78650a2018-07-30 19:41:25 +00001382FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001383 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001384 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001385 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001386 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001387 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001388}
1389
1390//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001391// GetElementPtrInst Implementation
1392//===----------------------------------------------------------------------===//
1393
Jay Foadd1b78492011-07-25 09:48:08 +00001394void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001395 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001396 assert(getNumOperands() == 1 + IdxList.size() &&
1397 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001398 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001399 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001400 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001401}
1402
Gabor Greiff6caff662008-05-10 08:32:32 +00001403GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001404 : Instruction(GEPI.getType(), GetElementPtr,
1405 OperandTraits<GetElementPtrInst>::op_end(this) -
1406 GEPI.getNumOperands(),
1407 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001408 SourceElementType(GEPI.SourceElementType),
1409 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001410 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001411 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001412}
1413
Chris Lattner6090a422009-03-09 04:46:40 +00001414/// getIndexedType - Returns the type of the element that would be accessed with
1415/// a gep instruction with the specified parameters.
1416///
1417/// The Idxs pointer should point to a continuous piece of memory containing the
1418/// indices, either as Value* or uint64_t.
1419///
1420/// A null type is returned if the indices are invalid for the specified
1421/// pointer type.
1422///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001423template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001424static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001425 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001426 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001427 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001428
Chris Lattner6090a422009-03-09 04:46:40 +00001429 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001430 // it cannot be 'stepped over'.
1431 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001432 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001433
Dan Gohman1ecaf452008-05-31 00:58:22 +00001434 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001435 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001436 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001437 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001438 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001439 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001440 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001441 }
Craig Topperc6207612014-04-09 06:08:46 +00001442 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001443}
1444
David Blaikied288fb82015-03-30 21:41:43 +00001445Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1446 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001447}
1448
David Blaikied288fb82015-03-30 21:41:43 +00001449Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001450 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001451 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001452}
1453
David Blaikied288fb82015-03-30 21:41:43 +00001454Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1455 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001456}
1457
Chris Lattner45f15572007-04-14 00:12:57 +00001458/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1459/// zeros. If so, the result pointer and the first operand have the same
1460/// value, just potentially different types.
1461bool GetElementPtrInst::hasAllZeroIndices() const {
1462 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1463 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1464 if (!CI->isZero()) return false;
1465 } else {
1466 return false;
1467 }
1468 }
1469 return true;
1470}
1471
Chris Lattner27058292007-04-27 20:35:56 +00001472/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1473/// constant integers. If so, the result pointer and the first operand have
1474/// a constant offset between them.
1475bool GetElementPtrInst::hasAllConstantIndices() const {
1476 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1477 if (!isa<ConstantInt>(getOperand(i)))
1478 return false;
1479 }
1480 return true;
1481}
1482
Dan Gohman1b849082009-09-07 23:54:19 +00001483void GetElementPtrInst::setIsInBounds(bool B) {
1484 cast<GEPOperator>(this)->setIsInBounds(B);
1485}
Chris Lattner45f15572007-04-14 00:12:57 +00001486
Nick Lewycky28a5f252009-09-27 21:33:04 +00001487bool GetElementPtrInst::isInBounds() const {
1488 return cast<GEPOperator>(this)->isInBounds();
1489}
1490
Chandler Carruth1e140532012-12-11 10:29:10 +00001491bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1492 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001493 // Delegate to the generic GEPOperator implementation.
1494 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001495}
1496
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001498// ExtractElementInst Implementation
1499//===----------------------------------------------------------------------===//
1500
1501ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001502 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001503 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001504 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001505 ExtractElement,
1506 OperandTraits<ExtractElementInst>::op_begin(this),
1507 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001508 assert(isValidOperands(Val, Index) &&
1509 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001510 Op<0>() = Val;
1511 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001512 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001513}
1514
1515ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001516 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001517 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001518 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001519 ExtractElement,
1520 OperandTraits<ExtractElementInst>::op_begin(this),
1521 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001522 assert(isValidOperands(Val, Index) &&
1523 "Invalid extractelement instruction operands!");
1524
Gabor Greif2d3024d2008-05-26 21:33:52 +00001525 Op<0>() = Val;
1526 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001527 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001528}
1529
Chris Lattner54865b32006-04-08 04:05:48 +00001530bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001531 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001532 return false;
1533 return true;
1534}
1535
Robert Bocchino23004482006-01-10 19:05:34 +00001536//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001537// InsertElementInst Implementation
1538//===----------------------------------------------------------------------===//
1539
Chris Lattner54865b32006-04-08 04:05:48 +00001540InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001541 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001542 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001543 : Instruction(Vec->getType(), InsertElement,
1544 OperandTraits<InsertElementInst>::op_begin(this),
1545 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001546 assert(isValidOperands(Vec, Elt, Index) &&
1547 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001548 Op<0>() = Vec;
1549 Op<1>() = Elt;
1550 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001551 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001552}
1553
Chris Lattner54865b32006-04-08 04:05:48 +00001554InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001555 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001556 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001557 : Instruction(Vec->getType(), InsertElement,
1558 OperandTraits<InsertElementInst>::op_begin(this),
1559 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001560 assert(isValidOperands(Vec, Elt, Index) &&
1561 "Invalid insertelement instruction operands!");
1562
Gabor Greif2d3024d2008-05-26 21:33:52 +00001563 Op<0>() = Vec;
1564 Op<1>() = Elt;
1565 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001566 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001567}
1568
Fangrui Songf78650a2018-07-30 19:41:25 +00001569bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
Chris Lattner54865b32006-04-08 04:05:48 +00001570 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001571 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001572 return false; // First operand of insertelement must be vector type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001573
Reid Spencerd84d35b2007-02-15 02:26:10 +00001574 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001575 return false;// Second operand of insertelement must be vector element type.
Fangrui Songf78650a2018-07-30 19:41:25 +00001576
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001577 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001578 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001579 return true;
1580}
1581
Robert Bocchinoca27f032006-01-17 20:07:22 +00001582//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001583// ShuffleVectorInst Implementation
1584//===----------------------------------------------------------------------===//
1585
1586ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001587 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001588 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001589: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001590 cast<VectorType>(Mask->getType())->getNumElements()),
1591 ShuffleVector,
1592 OperandTraits<ShuffleVectorInst>::op_begin(this),
1593 OperandTraits<ShuffleVectorInst>::operands(this),
1594 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001595 assert(isValidOperands(V1, V2, Mask) &&
1596 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001597 Op<0>() = V1;
1598 Op<1>() = V2;
1599 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001600 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001601}
1602
1603ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001604 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001605 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001606: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1607 cast<VectorType>(Mask->getType())->getNumElements()),
1608 ShuffleVector,
1609 OperandTraits<ShuffleVectorInst>::op_begin(this),
1610 OperandTraits<ShuffleVectorInst>::operands(this),
1611 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001612 assert(isValidOperands(V1, V2, Mask) &&
1613 "Invalid shuffle vector instruction operands!");
1614
Gabor Greif2d3024d2008-05-26 21:33:52 +00001615 Op<0>() = V1;
1616 Op<1>() = V2;
1617 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001618 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001619}
1620
Mon P Wang25f01062008-11-10 04:46:22 +00001621bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001622 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001623 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001624 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001625 return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00001626
Chris Lattner1dcb6542012-01-25 23:49:49 +00001627 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001628 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001629 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001630 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001631
1632 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001633 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1634 return true;
1635
Sanjay Patel8bd52282017-04-19 16:22:19 +00001636 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001637 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001638 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001639 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001640 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001641 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001642 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001643 return false;
1644 }
1645 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001646 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001647 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001648
Sanjay Patel8bd52282017-04-19 16:22:19 +00001649 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001650 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1651 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1652 if (CDS->getElementAsInteger(i) >= V1Size*2)
1653 return false;
1654 return true;
1655 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001656
Chris Lattner1dcb6542012-01-25 23:49:49 +00001657 // The bitcode reader can create a place holder for a forward reference
1658 // used as the shuffle mask. When this occurs, the shuffle mask will
1659 // fall into this case and fail. To avoid this error, do this bit of
1660 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001661 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001662 if (CE->getOpcode() == Instruction::UserOp1)
1663 return true;
1664
1665 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001666}
1667
Sanjay Patel2ca33602018-06-19 18:44:00 +00001668int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) {
Chris Lattnercf129702012-01-26 02:51:13 +00001669 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001670 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001671 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001672 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001673 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001674 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001675 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001676}
1677
Sanjay Patel2ca33602018-06-19 18:44:00 +00001678void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
Chris Lattnercf129702012-01-26 02:51:13 +00001679 SmallVectorImpl<int> &Result) {
1680 unsigned NumElts = Mask->getType()->getVectorNumElements();
Fangrui Songf78650a2018-07-30 19:41:25 +00001681
Sanjay Patel8bd52282017-04-19 16:22:19 +00001682 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001683 for (unsigned i = 0; i != NumElts; ++i)
1684 Result.push_back(CDS->getElementAsInteger(i));
1685 return;
Fangrui Songf78650a2018-07-30 19:41:25 +00001686 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001687 for (unsigned i = 0; i != NumElts; ++i) {
1688 Constant *C = Mask->getAggregateElement(i);
1689 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001690 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001691 }
1692}
1693
Sanjay Patelac619a02018-08-30 15:05:38 +00001694static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001695 assert(!Mask.empty() && "Shuffle mask must contain elements");
1696 bool UsesLHS = false;
1697 bool UsesRHS = false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001698 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001699 if (Mask[i] == -1)
1700 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001701 assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) &&
Sanjay Patel2ca33602018-06-19 18:44:00 +00001702 "Out-of-bounds shuffle mask element");
Sanjay Patelac619a02018-08-30 15:05:38 +00001703 UsesLHS |= (Mask[i] < NumOpElts);
1704 UsesRHS |= (Mask[i] >= NumOpElts);
Sanjay Patel2ca33602018-06-19 18:44:00 +00001705 if (UsesLHS && UsesRHS)
1706 return false;
1707 }
1708 assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source");
1709 return true;
1710}
1711
Sanjay Patelac619a02018-08-30 15:05:38 +00001712bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
1713 // We don't have vector operand size information, so assume operands are the
1714 // same size as the mask.
1715 return isSingleSourceMaskImpl(Mask, Mask.size());
1716}
1717
1718static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
1719 if (!isSingleSourceMaskImpl(Mask, NumOpElts))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001720 return false;
Sanjay Patelac619a02018-08-30 15:05:38 +00001721 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) {
Sanjay Patel2ca33602018-06-19 18:44:00 +00001722 if (Mask[i] == -1)
1723 continue;
Sanjay Patelac619a02018-08-30 15:05:38 +00001724 if (Mask[i] != i && Mask[i] != (NumOpElts + i))
Sanjay Patel2ca33602018-06-19 18:44:00 +00001725 return false;
1726 }
1727 return true;
1728}
1729
Sanjay Patelac619a02018-08-30 15:05:38 +00001730bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
1731 // We don't have vector operand size information, so assume operands are the
1732 // same size as the mask.
1733 return isIdentityMaskImpl(Mask, Mask.size());
1734}
1735
Sanjay Patel2ca33602018-06-19 18:44:00 +00001736bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
1737 if (!isSingleSourceMask(Mask))
1738 return false;
1739 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1740 if (Mask[i] == -1)
1741 continue;
1742 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
1743 return false;
1744 }
1745 return true;
1746}
1747
1748bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
1749 if (!isSingleSourceMask(Mask))
1750 return false;
1751 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1752 if (Mask[i] == -1)
1753 continue;
1754 if (Mask[i] != 0 && Mask[i] != NumElts)
1755 return false;
1756 }
1757 return true;
1758}
1759
1760bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
1761 // Select is differentiated from identity. It requires using both sources.
1762 if (isSingleSourceMask(Mask))
1763 return false;
1764 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
1765 if (Mask[i] == -1)
1766 continue;
1767 if (Mask[i] != i && Mask[i] != (NumElts + i))
1768 return false;
1769 }
1770 return true;
1771}
1772
1773bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
1774 // Example masks that will return true:
1775 // v1 = <a, b, c, d>
1776 // v2 = <e, f, g, h>
1777 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
1778 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
1779
1780 // 1. The number of elements in the mask must be a power-of-2 and at least 2.
1781 int NumElts = Mask.size();
1782 if (NumElts < 2 || !isPowerOf2_32(NumElts))
1783 return false;
1784
1785 // 2. The first element of the mask must be either a 0 or a 1.
1786 if (Mask[0] != 0 && Mask[0] != 1)
1787 return false;
1788
1789 // 3. The difference between the first 2 elements must be equal to the
1790 // number of elements in the mask.
1791 if ((Mask[1] - Mask[0]) != NumElts)
1792 return false;
1793
1794 // 4. The difference between consecutive even-numbered and odd-numbered
1795 // elements must be equal to 2.
1796 for (int i = 2; i < NumElts; ++i) {
1797 int MaskEltVal = Mask[i];
1798 if (MaskEltVal == -1)
1799 return false;
1800 int MaskEltPrevVal = Mask[i - 2];
1801 if (MaskEltVal - MaskEltPrevVal != 2)
1802 return false;
1803 }
1804 return true;
1805}
1806
Sanjay Patelac619a02018-08-30 15:05:38 +00001807bool ShuffleVectorInst::isIdentityWithPadding() const {
1808 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1809 int NumMaskElts = getType()->getVectorNumElements();
1810 if (NumMaskElts <= NumOpElts)
1811 return false;
1812
1813 // The first part of the mask must choose elements from exactly 1 source op.
Sanjay Patel8d39ed82018-08-30 16:44:07 +00001814 SmallVector<int, 16> Mask = getShuffleMask();
Sanjay Patelac619a02018-08-30 15:05:38 +00001815 if (!isIdentityMaskImpl(Mask, NumOpElts))
1816 return false;
1817
1818 // All extending must be with undef elements.
1819 for (int i = NumOpElts; i < NumMaskElts; ++i)
1820 if (Mask[i] != -1)
1821 return false;
1822
1823 return true;
1824}
1825
1826bool ShuffleVectorInst::isIdentityWithExtract() const {
1827 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1828 int NumMaskElts = getType()->getVectorNumElements();
1829 if (NumMaskElts >= NumOpElts)
1830 return false;
1831
1832 return isIdentityMaskImpl(getShuffleMask(), NumOpElts);
1833}
Sanjay Patel2ca33602018-06-19 18:44:00 +00001834
Sanjay Patelfd4976b2018-09-20 15:21:52 +00001835bool ShuffleVectorInst::isConcat() const {
1836 // Vector concatenation is differentiated from identity with padding.
1837 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()))
1838 return false;
1839
1840 int NumOpElts = Op<0>()->getType()->getVectorNumElements();
1841 int NumMaskElts = getType()->getVectorNumElements();
1842 if (NumMaskElts != NumOpElts * 2)
1843 return false;
1844
1845 // Use the mask length rather than the operands' vector lengths here. We
1846 // already know that the shuffle returns a vector twice as long as the inputs,
1847 // and neither of the inputs are undef vectors. If the mask picks consecutive
1848 // elements from both inputs, then this is a concatenation of the inputs.
1849 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts);
1850}
1851
Dan Gohman12fce772008-05-15 19:50:34 +00001852//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001853// InsertValueInst Class
1854//===----------------------------------------------------------------------===//
1855
Fangrui Songf78650a2018-07-30 19:41:25 +00001856void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
Jay Foad57aa6362011-07-13 10:26:04 +00001857 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001858 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001859
1860 // There's no fundamental reason why we require at least one index
1861 // (other than weirdness with &*IdxBegin being invalid; see
1862 // getelementptr's init routine for example). But there's no
1863 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001864 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001865
1866 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001867 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001868 Op<0>() = Agg;
1869 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001870
Jay Foad57aa6362011-07-13 10:26:04 +00001871 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001872 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001873}
1874
1875InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001876 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001877 OperandTraits<InsertValueInst>::op_begin(this), 2),
1878 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001879 Op<0>() = IVI.getOperand(0);
1880 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001881 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001882}
1883
1884//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001885// ExtractValueInst Class
1886//===----------------------------------------------------------------------===//
1887
Jay Foad57aa6362011-07-13 10:26:04 +00001888void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001889 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001890
Jay Foad57aa6362011-07-13 10:26:04 +00001891 // There's no fundamental reason why we require at least one index.
1892 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001893 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001894
Jay Foad57aa6362011-07-13 10:26:04 +00001895 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001896 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001897}
1898
1899ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001900 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001901 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001902 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001903}
1904
Dan Gohman12fce772008-05-15 19:50:34 +00001905// getIndexedType - Returns the type of the element that would be extracted
1906// with an extractvalue instruction with the specified parameters.
1907//
1908// A null type is returned if the indices are invalid for the specified
1909// pointer type.
1910//
Chris Lattner229907c2011-07-18 04:54:35 +00001911Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001912 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001913 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001914 // We can't use CompositeType::indexValid(Index) here.
1915 // indexValid() always returns true for arrays because getelementptr allows
1916 // out-of-bounds indices. Since we don't allow those for extractvalue and
1917 // insertvalue we need to check array indexing manually.
1918 // Since the only other types we can index into are struct types it's just
1919 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001920 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001921 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001922 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001923 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001924 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001925 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001926 } else {
1927 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001928 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001929 }
1930
1931 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001932 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001934}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001935
1936//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001937// BinaryOperator Class
1938//===----------------------------------------------------------------------===//
1939
Chris Lattner2195fc42007-02-24 00:55:48 +00001940BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001941 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001942 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001943 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001944 OperandTraits<BinaryOperator>::op_begin(this),
1945 OperandTraits<BinaryOperator>::operands(this),
1946 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001947 Op<0>() = S1;
1948 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001949 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001950 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001951}
1952
Fangrui Songf78650a2018-07-30 19:41:25 +00001953BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001954 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001955 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001956 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001957 OperandTraits<BinaryOperator>::op_begin(this),
1958 OperandTraits<BinaryOperator>::operands(this),
1959 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001960 Op<0>() = S1;
1961 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001962 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001963 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001964}
1965
Craig Topper700892f2017-06-26 07:15:59 +00001966void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001967 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001968 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001969 assert(LHS->getType() == RHS->getType() &&
1970 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001971#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00001972 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001973 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001974 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001975 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001976 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001977 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001978 "Tried to create an integer operation on a non-integer type!");
1979 break;
1980 case FAdd: case FSub:
1981 case FMul:
1982 assert(getType() == LHS->getType() &&
1983 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001984 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001985 "Tried to create a floating-point operation on a "
1986 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00001988 case UDiv:
1989 case SDiv:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001990 assert(getType() == LHS->getType() &&
1991 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001992 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001993 "Incorrect operand type (not integer) for S/UDIV");
1994 break;
1995 case FDiv:
1996 assert(getType() == LHS->getType() &&
1997 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001998 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001999 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002000 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002001 case URem:
2002 case SRem:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002003 assert(getType() == LHS->getType() &&
2004 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002005 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002006 "Incorrect operand type (not integer) for S/UREM");
2007 break;
2008 case FRem:
2009 assert(getType() == LHS->getType() &&
2010 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002011 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002012 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002013 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002014 case Shl:
2015 case LShr:
2016 case AShr:
2017 assert(getType() == LHS->getType() &&
2018 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002019 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002020 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002021 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002022 case And: case Or:
2023 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002024 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002025 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00002026 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002027 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002028 break;
Craig Topper700892f2017-06-26 07:15:59 +00002029 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002030 }
2031#endif
2032}
2033
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002034BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002035 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002036 Instruction *InsertBefore) {
2037 assert(S1->getType() == S2->getType() &&
2038 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002039 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002040}
2041
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002042BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002043 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002044 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002045 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002046 InsertAtEnd->getInstList().push_back(Res);
2047 return Res;
2048}
2049
Dan Gohman5476cfd2009-08-12 16:23:25 +00002050BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002052 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002053 return new BinaryOperator(Instruction::Sub,
2054 zero, Op,
2055 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002056}
2057
Dan Gohman5476cfd2009-08-12 16:23:25 +00002058BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002059 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002060 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002061 return new BinaryOperator(Instruction::Sub,
2062 zero, Op,
2063 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002064}
2065
Dan Gohman4ab44202009-12-18 02:58:50 +00002066BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2067 Instruction *InsertBefore) {
2068 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2069 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2070}
2071
2072BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2073 BasicBlock *InsertAtEnd) {
2074 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2075 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2076}
2077
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002078BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2079 Instruction *InsertBefore) {
2080 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2081 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2082}
2083
2084BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2085 BasicBlock *InsertAtEnd) {
2086 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2087 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2088}
2089
Dan Gohman5476cfd2009-08-12 16:23:25 +00002090BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002091 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002092 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002093 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002094 Op->getType(), Name, InsertBefore);
2095}
2096
Dan Gohman5476cfd2009-08-12 16:23:25 +00002097BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002098 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002099 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002100 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002101 Op->getType(), Name, InsertAtEnd);
2102}
2103
Dan Gohman5476cfd2009-08-12 16:23:25 +00002104BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002105 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002106 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002107 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002108 Op->getType(), Name, InsertBefore);
2109}
2110
Dan Gohman5476cfd2009-08-12 16:23:25 +00002111BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002113 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002114 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 Op->getType(), Name, InsertAtEnd);
2116}
2117
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118// isConstantAllOnes - Helper function for several functions below
2119static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002120 if (const Constant *C = dyn_cast<Constant>(V))
2121 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002122 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002123}
2124
Owen Andersonbb2501b2009-07-13 22:18:28 +00002125bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002126 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2127 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002128 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002129 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002130 return false;
2131}
2132
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002133bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002134 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2135 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002136 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002137 if (!IgnoreZeroSign)
2138 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2139 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2140 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002141 return false;
2142}
2143
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002144bool BinaryOperator::isNot(const Value *V) {
2145 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2146 return (Bop->getOpcode() == Instruction::Xor &&
2147 (isConstantAllOnes(Bop->getOperand(1)) ||
2148 isConstantAllOnes(Bop->getOperand(0))));
2149 return false;
2150}
2151
Chris Lattner2c7d1772005-04-24 07:28:37 +00002152Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002153 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002154}
2155
Chris Lattner2c7d1772005-04-24 07:28:37 +00002156const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2157 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002158}
2159
Dan Gohmana5b96452009-06-04 22:49:04 +00002160Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002161 return cast<BinaryOperator>(BinOp)->getOperand(1);
2162}
2163
2164const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2165 return getFNegArgument(const_cast<Value*>(BinOp));
2166}
2167
Chris Lattner2c7d1772005-04-24 07:28:37 +00002168Value *BinaryOperator::getNotArgument(Value *BinOp) {
2169 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2170 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2171 Value *Op0 = BO->getOperand(0);
2172 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002173 if (isConstantAllOnes(Op0)) return Op1;
2174
2175 assert(isConstantAllOnes(Op1));
2176 return Op0;
2177}
2178
Chris Lattner2c7d1772005-04-24 07:28:37 +00002179const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2180 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181}
2182
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002183// Exchange the two operands to this instruction. This instruction is safe to
2184// use on any binary instruction and does not modify the semantics of the
2185// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2186// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002187bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002188 if (!isCommutative())
2189 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002190 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002191 return false;
2192}
2193
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002194//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002195// FPMathOperator Class
2196//===----------------------------------------------------------------------===//
2197
Duncan Sands05f4df82012-04-16 16:28:59 +00002198float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002199 const MDNode *MD =
2200 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002201 if (!MD)
2202 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002203 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002204 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002205}
2206
Duncan Sands05f4df82012-04-16 16:28:59 +00002207//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002208// CastInst Class
2209//===----------------------------------------------------------------------===//
2210
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211// Just determine if this cast only deals with integral->integral conversion.
2212bool CastInst::isIntegerCast() const {
2213 switch (getOpcode()) {
2214 default: return false;
2215 case Instruction::ZExt:
2216 case Instruction::SExt:
2217 case Instruction::Trunc:
2218 return true;
2219 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002220 return getOperand(0)->getType()->isIntegerTy() &&
2221 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002223}
2224
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225bool CastInst::isLosslessCast() const {
2226 // Only BitCast can be lossless, exit fast if we're not BitCast
2227 if (getOpcode() != Instruction::BitCast)
2228 return false;
2229
2230 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002231 Type *SrcTy = getOperand(0)->getType();
2232 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 if (SrcTy == DstTy)
2234 return true;
Fangrui Songf78650a2018-07-30 19:41:25 +00002235
Reid Spencer8d9336d2006-12-31 05:26:44 +00002236 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002237 if (SrcTy->isPointerTy())
2238 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 return false; // Other types have no identity values
2240}
2241
2242/// This function determines if the CastInst does not require any bits to be
2243/// changed in order to effect the cast. Essentially, it identifies cases where
Fangrui Songf78650a2018-07-30 19:41:25 +00002244/// no code gen is necessary for the cast, hence the name no-op cast. For
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002245/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002246/// # bitcast i32* %x to i8*
Fangrui Songf78650a2018-07-30 19:41:25 +00002247/// # bitcast <2 x i32> %x to <4 x i16>
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002248/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002249/// Determine if the described cast is a no-op.
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002250bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002251 Type *SrcTy,
2252 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002253 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002254 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002255 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 case Instruction::Trunc:
2257 case Instruction::ZExt:
Fangrui Songf78650a2018-07-30 19:41:25 +00002258 case Instruction::SExt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259 case Instruction::FPTrunc:
2260 case Instruction::FPExt:
2261 case Instruction::UIToFP:
2262 case Instruction::SIToFP:
2263 case Instruction::FPToUI:
2264 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002265 case Instruction::AddrSpaceCast:
2266 // TODO: Target informations may give a more accurate answer here.
2267 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268 case Instruction::BitCast:
2269 return true; // BitCast never modifies bits.
2270 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002271 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002272 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002274 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002275 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276 }
2277}
2278
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002279bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002280 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002281}
2282
2283/// This function determines if a pair of casts can be eliminated and what
2284/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285/// instructions like this:
2286/// * %F = firstOpcode SrcTy %x to MidTy
2287/// * %S = secondOpcode MidTy %F to DstTy
2288/// The function returns a resultOpcode so these two casts can be replaced with:
2289/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002290/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291unsigned CastInst::isEliminableCastPair(
2292 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002293 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2294 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 // Define the 144 possibilities for these two cast instructions. The values
2296 // in this matrix determine what to do in a given situation and select the
Fangrui Songf78650a2018-07-30 19:41:25 +00002297 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002298 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 // the following cast properties:
2300 //
2301 // Size Compare Source Destination
2302 // Operator Src ? Size Type Sign Type Sign
2303 // -------- ------------ ------------------- ---------------------
2304 // TRUNC > Integer Any Integral Any
2305 // ZEXT < Integral Unsigned Integer Any
2306 // SEXT < Integral Signed Integer Any
2307 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002308 // FPTOSI n/a FloatPt n/a Integral Signed
2309 // UITOFP n/a Integral Unsigned FloatPt n/a
2310 // SITOFP n/a Integral Signed FloatPt n/a
2311 // FPTRUNC > FloatPt n/a FloatPt n/a
2312 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 // PTRTOINT n/a Pointer n/a Integral Unsigned
2314 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002315 // BITCAST = FirstClass n/a FirstClass n/a
2316 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002317 //
2318 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002319 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2320 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002321 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002322 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002323 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002324 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002325 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2327 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002328 // T F F U S F F P I B A -+
2329 // R Z S P P I I T P 2 N T S |
2330 // U E E 2 2 2 2 R E I T C C +- secondOp
2331 // N X X U S F F N X N 2 V V |
2332 // C T T I I P P C T T P T T -+
2333 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002334 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002335 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2336 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2337 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2338 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2339 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002340 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002341 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002342 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2343 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2344 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2345 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002347
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002348 // TODO: This logic could be encoded into the table above and handled in the
2349 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002350 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002351 // merging. However, any pair of bitcasts are allowed.
2352 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2353 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2354 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002355
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002356 // Check if any of the casts convert scalars <-> vectors.
2357 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2358 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2359 if (!AreBothBitcasts)
2360 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361
2362 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2363 [secondOp-Instruction::CastOpsBegin];
2364 switch (ElimCase) {
Fangrui Songf78650a2018-07-30 19:41:25 +00002365 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002366 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002368 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002369 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 return firstOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002371 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002372 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 return secondOp;
Fangrui Songf78650a2018-07-30 19:41:25 +00002374 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002375 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002376 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002377 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002378 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 return firstOp;
2380 return 0;
2381 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002382 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002383 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002384 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 return firstOp;
2386 return 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002387 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002388 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002389 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002390 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 return secondOp;
2392 return 0;
2393 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002394 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002395 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002396 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 return secondOp;
2398 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002399 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002400 // Cannot simplify if address spaces are different!
2401 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2402 return 0;
2403
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002404 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002405 // We can still fold this without knowing the actual sizes as long we
2406 // know that the intermediate pointer is the largest possible
2407 // pointer size.
2408 // FIXME: Is this always true?
2409 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002410 return Instruction::BitCast;
2411
2412 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002413 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002414 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002415 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 if (MidSize >= PtrSize)
2417 return Instruction::BitCast;
2418 return 0;
2419 }
2420 case 8: {
2421 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2422 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2423 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002424 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2425 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 if (SrcSize == DstSize)
2427 return Instruction::BitCast;
2428 else if (SrcSize < DstSize)
2429 return firstOp;
2430 return secondOp;
2431 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002432 case 9:
2433 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002435 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002437 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002438 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002439 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002440 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2441 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 if (SrcSize <= PtrSize && SrcSize == DstSize)
2443 return Instruction::BitCast;
2444 return 0;
2445 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002446 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002447 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2448 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2449 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2450 return Instruction::AddrSpaceCast;
2451 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002452 case 13:
2453 // FIXME: this state can be merged with (1), but the following assert
2454 // is useful to check the correcteness of the sequence due to semantic
2455 // change of bitcast.
2456 assert(
2457 SrcTy->isPtrOrPtrVectorTy() &&
2458 MidTy->isPtrOrPtrVectorTy() &&
2459 DstTy->isPtrOrPtrVectorTy() &&
2460 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2461 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2462 "Illegal addrspacecast, bitcast sequence!");
2463 // Allowed, use first cast's opcode
2464 return firstOp;
2465 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002466 // bitcast, addrspacecast -> addrspacecast if the element type of
2467 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002468 if (SrcTy->getScalarType()->getPointerElementType() ==
2469 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002470 return Instruction::AddrSpaceCast;
2471 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002472 case 15:
2473 // FIXME: this state can be merged with (1), but the following assert
2474 // is useful to check the correcteness of the sequence due to semantic
2475 // change of bitcast.
2476 assert(
2477 SrcTy->isIntOrIntVectorTy() &&
2478 MidTy->isPtrOrPtrVectorTy() &&
2479 DstTy->isPtrOrPtrVectorTy() &&
2480 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2481 "Illegal inttoptr, bitcast sequence!");
2482 // Allowed, use first cast's opcode
2483 return firstOp;
2484 case 16:
2485 // FIXME: this state can be merged with (2), but the following assert
2486 // is useful to check the correcteness of the sequence due to semantic
2487 // change of bitcast.
2488 assert(
2489 SrcTy->isPtrOrPtrVectorTy() &&
2490 MidTy->isPtrOrPtrVectorTy() &&
2491 DstTy->isIntOrIntVectorTy() &&
2492 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2493 "Illegal bitcast, ptrtoint sequence!");
2494 // Allowed, use second cast's opcode
2495 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002496 case 17:
2497 // (sitofp (zext x)) -> (uitofp x)
2498 return Instruction::UIToFP;
Fangrui Songf78650a2018-07-30 19:41:25 +00002499 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002500 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002502 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503 default:
Craig Topperc514b542012-02-05 22:14:15 +00002504 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506}
2507
Fangrui Songf78650a2018-07-30 19:41:25 +00002508CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002509 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002510 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 // Construct and return the appropriate CastInst subclass
2512 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002513 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2514 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2515 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2516 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2517 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2518 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2519 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2520 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2521 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2522 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2523 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2524 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2525 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2526 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528}
2529
Chris Lattner229907c2011-07-18 04:54:35 +00002530CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002532 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533 // Construct and return the appropriate CastInst subclass
2534 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002535 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2536 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2537 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2538 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2539 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2540 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2541 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2542 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2543 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2544 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2545 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2546 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2547 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2548 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550}
2551
Fangrui Songf78650a2018-07-30 19:41:25 +00002552CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002553 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002554 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002555 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002556 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2557 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002558}
2559
Fangrui Songf78650a2018-07-30 19:41:25 +00002560CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002562 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002563 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002564 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2565 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002566}
2567
Fangrui Songf78650a2018-07-30 19:41:25 +00002568CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002569 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002570 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002571 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002572 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2573 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002574}
2575
Fangrui Songf78650a2018-07-30 19:41:25 +00002576CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002578 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002579 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002580 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2581 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002582}
2583
Chris Lattner229907c2011-07-18 04:54:35 +00002584CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002586 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002587 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002588 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2589 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002590}
2591
Chris Lattner229907c2011-07-18 04:54:35 +00002592CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Fangrui Songf78650a2018-07-30 19:41:25 +00002593 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002594 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002595 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002596 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2597 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002598}
2599
Chris Lattner229907c2011-07-18 04:54:35 +00002600CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002601 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002602 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002603 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2604 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2605 "Invalid cast");
2606 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002607 assert((!Ty->isVectorTy() ||
2608 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002609 "Invalid cast");
2610
Matt Arsenault065ced92013-07-31 00:17:33 +00002611 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002612 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002613
Matt Arsenault740980e2014-07-14 17:24:35 +00002614 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002615}
2616
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002617/// Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002618CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2619 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002620 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002621 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2622 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002623 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002624 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002625 assert((!Ty->isVectorTy() ||
2626 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002627 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002628
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002629 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002630 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002631
Matt Arsenault740980e2014-07-14 17:24:35 +00002632 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2633}
2634
2635CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2636 Value *S, Type *Ty,
2637 const Twine &Name,
2638 BasicBlock *InsertAtEnd) {
2639 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2640 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2641
2642 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2643 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2644
2645 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2646}
2647
2648CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2649 Value *S, Type *Ty,
2650 const Twine &Name,
2651 Instruction *InsertBefore) {
2652 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2653 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2654
2655 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002656 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2657
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002658 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002659}
2660
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002661CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2662 const Twine &Name,
2663 Instruction *InsertBefore) {
2664 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2665 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2666 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2667 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2668
2669 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2670}
2671
Matt Arsenault740980e2014-07-14 17:24:35 +00002672CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002673 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002674 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002675 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002676 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002677 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2678 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002679 Instruction::CastOps opcode =
2680 (SrcBits == DstBits ? Instruction::BitCast :
2681 (SrcBits > DstBits ? Instruction::Trunc :
2682 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002683 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002684}
2685
Fangrui Songf78650a2018-07-30 19:41:25 +00002686CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002687 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002688 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002689 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002690 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002691 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2692 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002693 Instruction::CastOps opcode =
2694 (SrcBits == DstBits ? Instruction::BitCast :
2695 (SrcBits > DstBits ? Instruction::Trunc :
2696 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002697 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002698}
2699
Fangrui Songf78650a2018-07-30 19:41:25 +00002700CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2701 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002702 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002703 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002704 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002705 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2706 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002707 Instruction::CastOps opcode =
2708 (SrcBits == DstBits ? Instruction::BitCast :
2709 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002710 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002711}
2712
Fangrui Songf78650a2018-07-30 19:41:25 +00002713CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2714 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002715 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002716 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002717 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002718 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2719 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002720 Instruction::CastOps opcode =
2721 (SrcBits == DstBits ? Instruction::BitCast :
2722 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002723 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002724}
2725
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002726// Check whether it is valid to call getCastOpcode for these types.
2727// This routine must be kept in sync with getCastOpcode.
2728bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2729 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2730 return false;
2731
2732 if (SrcTy == DestTy)
2733 return true;
2734
2735 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2736 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2737 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2738 // An element by element cast. Valid if casting the elements is valid.
2739 SrcTy = SrcVecTy->getElementType();
2740 DestTy = DestVecTy->getElementType();
2741 }
2742
2743 // Get the bit sizes, we'll need these
2744 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2745 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2746
2747 // Run through the possibilities ...
2748 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002749 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002750 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002751 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002752 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002753 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002754 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002755 // Casting from something else
2756 return SrcTy->isPointerTy();
Fangrui Songf78650a2018-07-30 19:41:25 +00002757 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002758 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2759 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002760 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002761 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002762 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002763 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002764 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002765 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002766 return false;
2767 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002768 if (DestTy->isVectorTy()) // Casting to vector
2769 return DestBits == SrcBits;
2770 if (DestTy->isPointerTy()) { // Casting to pointer
2771 if (SrcTy->isPointerTy()) // Casting from pointer
2772 return true;
2773 return SrcTy->isIntegerTy(); // Casting from integral
Fangrui Songf78650a2018-07-30 19:41:25 +00002774 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002775 if (DestTy->isX86_MMXTy()) {
2776 if (SrcTy->isVectorTy())
2777 return DestBits == SrcBits; // 64-bit vector to MMX
2778 return false;
2779 } // Casting to something else
2780 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002781}
2782
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002783bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2784 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2785 return false;
2786
2787 if (SrcTy == DestTy)
2788 return true;
2789
2790 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2791 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2792 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2793 // An element by element cast. Valid if casting the elements is valid.
2794 SrcTy = SrcVecTy->getElementType();
2795 DestTy = DestVecTy->getElementType();
2796 }
2797 }
2798 }
2799
2800 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2801 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2802 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2803 }
2804 }
2805
2806 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2807 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2808
2809 // Could still have vectors of pointers if the number of elements doesn't
2810 // match
2811 if (SrcBits == 0 || DestBits == 0)
2812 return false;
2813
2814 if (SrcBits != DestBits)
2815 return false;
2816
2817 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2818 return false;
2819
2820 return true;
2821}
2822
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002823bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002824 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002825 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002826 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2827 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002828 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2829 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002830 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2831 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002832 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2833 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002834
2835 return isBitCastable(SrcTy, DestTy);
2836}
2837
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002838// Provide a way to get a "cast" where the cast opcode is inferred from the
2839// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002840// logic in the castIsValid function below. This axiom should hold:
2841// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2842// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002844// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002845Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002846CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002847 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2848 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002849
Duncan Sands55e50902008-01-06 10:12:28 +00002850 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2851 "Only first class types are castable!");
2852
Duncan Sandsa8514532011-05-18 07:13:41 +00002853 if (SrcTy == DestTy)
2854 return BitCast;
2855
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002856 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002857 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2858 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002859 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2860 // An element by element cast. Find the appropriate opcode based on the
2861 // element types.
2862 SrcTy = SrcVecTy->getElementType();
2863 DestTy = DestVecTy->getElementType();
2864 }
2865
2866 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002867 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2868 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002869
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002870 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002871 if (DestTy->isIntegerTy()) { // Casting to integral
2872 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002873 if (DestBits < SrcBits)
2874 return Trunc; // int -> smaller int
2875 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002876 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002877 return SExt; // signed -> SEXT
2878 else
2879 return ZExt; // unsigned -> ZEXT
2880 } else {
2881 return BitCast; // Same size, No-op cast
2882 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002883 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Fangrui Songf78650a2018-07-30 19:41:25 +00002884 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002885 return FPToSI; // FP -> sint
2886 else
Fangrui Songf78650a2018-07-30 19:41:25 +00002887 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002888 } else if (SrcTy->isVectorTy()) {
2889 assert(DestBits == SrcBits &&
2890 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002891 return BitCast; // Same size, no-op cast
2892 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002893 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002894 "Casting from a value that is not first-class type");
2895 return PtrToInt; // ptr -> int
2896 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002897 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2898 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002899 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002900 return SIToFP; // sint -> FP
2901 else
2902 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002903 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904 if (DestBits < SrcBits) {
2905 return FPTrunc; // FP -> smaller FP
2906 } else if (DestBits > SrcBits) {
2907 return FPExt; // FP -> larger FP
2908 } else {
2909 return BitCast; // same size, no-op cast
2910 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002911 } else if (SrcTy->isVectorTy()) {
2912 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002913 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002914 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002915 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002916 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002917 } else if (DestTy->isVectorTy()) {
2918 assert(DestBits == SrcBits &&
2919 "Illegal cast to vector (wrong type or size)");
2920 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002921 } else if (DestTy->isPointerTy()) {
2922 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002923 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2924 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002925 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002926 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002929 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002930 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002931 if (SrcTy->isVectorTy()) {
2932 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002933 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002934 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002935 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002936 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002937 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002938}
2939
2940//===----------------------------------------------------------------------===//
2941// CastInst SubClass Constructors
2942//===----------------------------------------------------------------------===//
2943
2944/// Check that the construction parameters for a CastInst are correct. This
2945/// could be broken out into the separate constructors but it is useful to have
2946/// it in one place and to eliminate the redundant code for getting the sizes
2947/// of the types involved.
Fangrui Songf78650a2018-07-30 19:41:25 +00002948bool
Chris Lattner229907c2011-07-18 04:54:35 +00002949CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002951 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002952
Chris Lattner37bc78a2010-01-26 21:51:43 +00002953 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2954 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002955 return false;
2956
2957 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002958 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2959 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002960
Duncan Sands7f646562011-05-18 09:21:57 +00002961 // If these are vector types, get the lengths of the vectors (using zero for
2962 // scalar types means that checking that vector lengths match also checks that
2963 // scalars are not being converted to vectors or vectors to scalars).
2964 unsigned SrcLength = SrcTy->isVectorTy() ?
2965 cast<VectorType>(SrcTy)->getNumElements() : 0;
2966 unsigned DstLength = DstTy->isVectorTy() ?
2967 cast<VectorType>(DstTy)->getNumElements() : 0;
2968
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969 // Switch on the opcode provided
2970 switch (op) {
2971 default: return false; // This is an input error
2972 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002973 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2974 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002976 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2977 SrcLength == DstLength && SrcBitSize < DstBitSize;
Fangrui Songf78650a2018-07-30 19:41:25 +00002978 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002979 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2980 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002982 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2983 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002985 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2986 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002988 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002989 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2990 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002992 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002993 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2994 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002996 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002997 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002998 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2999 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3000 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003001 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003002 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003003 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003004 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003005 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3006 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3007 return false;
Craig Topper95d23472017-07-09 07:04:00 +00003008 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003009 case Instruction::BitCast: {
3010 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3011 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3012
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013 // BitCast implies a no-op cast of type only. No bits change.
3014 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003015 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016 return false;
3017
Alp Tokerf907b892013-12-05 05:44:44 +00003018 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003019 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003020 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003021 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3022
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003023 // If both are pointers then the address spaces must match.
3024 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3025 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003026
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003027 // A vector of pointers must have the same number of elements.
Serguei Katkov09ab5062018-08-21 04:27:07 +00003028 VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy);
3029 VectorType *DstVecTy = dyn_cast<VectorType>(DstTy);
3030 if (SrcVecTy && DstVecTy)
3031 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3032 if (SrcVecTy)
3033 return SrcVecTy->getNumElements() == 1;
3034 if (DstVecTy)
3035 return DstVecTy->getNumElements() == 1;
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003036
3037 return true;
3038 }
3039 case Instruction::AddrSpaceCast: {
3040 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3041 if (!SrcPtrTy)
3042 return false;
3043
3044 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3045 if (!DstPtrTy)
3046 return false;
3047
3048 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3049 return false;
3050
3051 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3052 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3053 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3054
3055 return false;
3056 }
3057
3058 return true;
3059 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003060 }
3061}
3062
3063TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003064 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003065) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003066 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003067}
3068
3069TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003070 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003071) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003072 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003073}
3074
3075ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003076 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003077) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003078 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003079}
3080
3081ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003082 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003083) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003084 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003085}
3086SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003087 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003088) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003089 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003090}
3091
Jeff Cohencc08c832006-12-02 02:22:01 +00003092SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003093 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003094) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003095 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096}
3097
3098FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003099 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003100) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003101 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003102}
3103
3104FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003105 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003106) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003107 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003108}
3109
3110FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003111 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003112) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003113 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003114}
3115
3116FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003117 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003118) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003119 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003120}
3121
3122UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003123 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003124) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003125 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126}
3127
3128UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003129 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003130) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003131 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132}
3133
3134SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003135 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003136) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003137 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138}
3139
3140SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003141 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003142) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003143 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003144}
3145
3146FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003147 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003148) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003149 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150}
3151
3152FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003153 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003154) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003155 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156}
3157
3158FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003159 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003160) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003161 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162}
3163
3164FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003166) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003172) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003178) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003184) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003190) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193
3194BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Fangrui Songf78650a2018-07-30 19:41:25 +00003196) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
3200BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Fangrui Songf78650a2018-07-30 19:41:25 +00003202) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003205
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003206AddrSpaceCastInst::AddrSpaceCastInst(
3207 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3208) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3210}
3211
3212AddrSpaceCastInst::AddrSpaceCastInst(
3213 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3214) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3216}
3217
Chris Lattnerf16dc002006-09-17 19:29:56 +00003218//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003219// CmpInst Classes
3220//===----------------------------------------------------------------------===//
3221
Craig Topper1c3f2832015-12-15 06:11:33 +00003222CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3223 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003224 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003225 OperandTraits<CmpInst>::op_begin(this),
3226 OperandTraits<CmpInst>::operands(this),
3227 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003228 Op<0>() = LHS;
3229 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003230 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003231 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003232}
Gabor Greiff6caff662008-05-10 08:32:32 +00003233
Craig Topper1c3f2832015-12-15 06:11:33 +00003234CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3235 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003236 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003237 OperandTraits<CmpInst>::op_begin(this),
3238 OperandTraits<CmpInst>::operands(this),
3239 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003240 Op<0>() = LHS;
3241 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003242 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003243 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003244}
3245
3246CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003247CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003248 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003249 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003250 if (InsertBefore)
3251 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3252 S1, S2, Name);
3253 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003254 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003255 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003256 }
Fangrui Songf78650a2018-07-30 19:41:25 +00003257
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258 if (InsertBefore)
3259 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3260 S1, S2, Name);
3261 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003262 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003263 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003264}
3265
3266CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003267CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003268 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003269 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003270 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3271 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003272 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003273 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3274 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003275}
3276
3277void CmpInst::swapOperands() {
3278 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3279 IC->swapOperands();
3280 else
3281 cast<FCmpInst>(this)->swapOperands();
3282}
3283
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003284bool CmpInst::isCommutative() const {
3285 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003286 return IC->isCommutative();
3287 return cast<FCmpInst>(this)->isCommutative();
3288}
3289
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003290bool CmpInst::isEquality() const {
3291 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003292 return IC->isEquality();
3293 return cast<FCmpInst>(this)->isEquality();
3294}
3295
Dan Gohman4e724382008-05-31 02:47:54 +00003296CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003297 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003298 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003299 case ICMP_EQ: return ICMP_NE;
3300 case ICMP_NE: return ICMP_EQ;
3301 case ICMP_UGT: return ICMP_ULE;
3302 case ICMP_ULT: return ICMP_UGE;
3303 case ICMP_UGE: return ICMP_ULT;
3304 case ICMP_ULE: return ICMP_UGT;
3305 case ICMP_SGT: return ICMP_SLE;
3306 case ICMP_SLT: return ICMP_SGE;
3307 case ICMP_SGE: return ICMP_SLT;
3308 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003309
Dan Gohman4e724382008-05-31 02:47:54 +00003310 case FCMP_OEQ: return FCMP_UNE;
3311 case FCMP_ONE: return FCMP_UEQ;
3312 case FCMP_OGT: return FCMP_ULE;
3313 case FCMP_OLT: return FCMP_UGE;
3314 case FCMP_OGE: return FCMP_ULT;
3315 case FCMP_OLE: return FCMP_UGT;
3316 case FCMP_UEQ: return FCMP_ONE;
3317 case FCMP_UNE: return FCMP_OEQ;
3318 case FCMP_UGT: return FCMP_OLE;
3319 case FCMP_ULT: return FCMP_OGE;
3320 case FCMP_UGE: return FCMP_OLT;
3321 case FCMP_ULE: return FCMP_OGT;
3322 case FCMP_ORD: return FCMP_UNO;
3323 case FCMP_UNO: return FCMP_ORD;
3324 case FCMP_TRUE: return FCMP_FALSE;
3325 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003326 }
3327}
3328
Tim Northoverde3aea0412016-08-17 20:25:25 +00003329StringRef CmpInst::getPredicateName(Predicate Pred) {
3330 switch (Pred) {
3331 default: return "unknown";
3332 case FCmpInst::FCMP_FALSE: return "false";
3333 case FCmpInst::FCMP_OEQ: return "oeq";
3334 case FCmpInst::FCMP_OGT: return "ogt";
3335 case FCmpInst::FCMP_OGE: return "oge";
3336 case FCmpInst::FCMP_OLT: return "olt";
3337 case FCmpInst::FCMP_OLE: return "ole";
3338 case FCmpInst::FCMP_ONE: return "one";
3339 case FCmpInst::FCMP_ORD: return "ord";
3340 case FCmpInst::FCMP_UNO: return "uno";
3341 case FCmpInst::FCMP_UEQ: return "ueq";
3342 case FCmpInst::FCMP_UGT: return "ugt";
3343 case FCmpInst::FCMP_UGE: return "uge";
3344 case FCmpInst::FCMP_ULT: return "ult";
3345 case FCmpInst::FCMP_ULE: return "ule";
3346 case FCmpInst::FCMP_UNE: return "une";
3347 case FCmpInst::FCMP_TRUE: return "true";
3348 case ICmpInst::ICMP_EQ: return "eq";
3349 case ICmpInst::ICMP_NE: return "ne";
3350 case ICmpInst::ICMP_SGT: return "sgt";
3351 case ICmpInst::ICMP_SGE: return "sge";
3352 case ICmpInst::ICMP_SLT: return "slt";
3353 case ICmpInst::ICMP_SLE: return "sle";
3354 case ICmpInst::ICMP_UGT: return "ugt";
3355 case ICmpInst::ICMP_UGE: return "uge";
3356 case ICmpInst::ICMP_ULT: return "ult";
3357 case ICmpInst::ICMP_ULE: return "ule";
3358 }
3359}
3360
Reid Spencer266e42b2006-12-23 06:05:41 +00003361ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3362 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003363 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003364 case ICMP_EQ: case ICMP_NE:
3365 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003366 return pred;
3367 case ICMP_UGT: return ICMP_SGT;
3368 case ICMP_ULT: return ICMP_SLT;
3369 case ICMP_UGE: return ICMP_SGE;
3370 case ICMP_ULE: return ICMP_SLE;
3371 }
3372}
3373
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003374ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3375 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003376 default: llvm_unreachable("Unknown icmp predicate!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003377 case ICMP_EQ: case ICMP_NE:
3378 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003379 return pred;
3380 case ICMP_SGT: return ICMP_UGT;
3381 case ICMP_SLT: return ICMP_ULT;
3382 case ICMP_SGE: return ICMP_UGE;
3383 case ICMP_SLE: return ICMP_ULE;
3384 }
3385}
3386
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003387CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3388 switch (pred) {
3389 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3390 case ICMP_SGT: return ICMP_SGE;
3391 case ICMP_SLT: return ICMP_SLE;
3392 case ICMP_SGE: return ICMP_SGT;
3393 case ICMP_SLE: return ICMP_SLT;
3394 case ICMP_UGT: return ICMP_UGE;
3395 case ICMP_ULT: return ICMP_ULE;
3396 case ICMP_UGE: return ICMP_UGT;
3397 case ICMP_ULE: return ICMP_ULT;
3398
3399 case FCMP_OGT: return FCMP_OGE;
3400 case FCMP_OLT: return FCMP_OLE;
3401 case FCMP_OGE: return FCMP_OGT;
3402 case FCMP_OLE: return FCMP_OLT;
3403 case FCMP_UGT: return FCMP_UGE;
3404 case FCMP_ULT: return FCMP_ULE;
3405 case FCMP_UGE: return FCMP_UGT;
3406 case FCMP_ULE: return FCMP_ULT;
3407 }
3408}
3409
Dan Gohman4e724382008-05-31 02:47:54 +00003410CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003411 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003412 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003413 case ICMP_EQ: case ICMP_NE:
3414 return pred;
3415 case ICMP_SGT: return ICMP_SLT;
3416 case ICMP_SLT: return ICMP_SGT;
3417 case ICMP_SGE: return ICMP_SLE;
3418 case ICMP_SLE: return ICMP_SGE;
3419 case ICMP_UGT: return ICMP_ULT;
3420 case ICMP_ULT: return ICMP_UGT;
3421 case ICMP_UGE: return ICMP_ULE;
3422 case ICMP_ULE: return ICMP_UGE;
Fangrui Songf78650a2018-07-30 19:41:25 +00003423
Reid Spencerd9436b62006-11-20 01:22:35 +00003424 case FCMP_FALSE: case FCMP_TRUE:
3425 case FCMP_OEQ: case FCMP_ONE:
3426 case FCMP_UEQ: case FCMP_UNE:
3427 case FCMP_ORD: case FCMP_UNO:
3428 return pred;
3429 case FCMP_OGT: return FCMP_OLT;
3430 case FCMP_OLT: return FCMP_OGT;
3431 case FCMP_OGE: return FCMP_OLE;
3432 case FCMP_OLE: return FCMP_OGE;
3433 case FCMP_UGT: return FCMP_ULT;
3434 case FCMP_ULT: return FCMP_UGT;
3435 case FCMP_UGE: return FCMP_ULE;
3436 case FCMP_ULE: return FCMP_UGE;
3437 }
3438}
3439
Max Kazantsevb299ade2018-02-07 11:16:29 +00003440CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3441 switch (pred) {
3442 case ICMP_SGT: return ICMP_SGE;
3443 case ICMP_SLT: return ICMP_SLE;
3444 case ICMP_UGT: return ICMP_UGE;
3445 case ICMP_ULT: return ICMP_ULE;
3446 case FCMP_OGT: return FCMP_OGE;
3447 case FCMP_OLT: return FCMP_OLE;
3448 case FCMP_UGT: return FCMP_UGE;
3449 case FCMP_ULT: return FCMP_ULE;
3450 default: return pred;
3451 }
3452}
3453
Sanjoy Das6e78b172015-10-22 19:57:34 +00003454CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3455 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3456
3457 switch (pred) {
3458 default:
3459 llvm_unreachable("Unknown predicate!");
3460 case CmpInst::ICMP_ULT:
3461 return CmpInst::ICMP_SLT;
3462 case CmpInst::ICMP_ULE:
3463 return CmpInst::ICMP_SLE;
3464 case CmpInst::ICMP_UGT:
3465 return CmpInst::ICMP_SGT;
3466 case CmpInst::ICMP_UGE:
3467 return CmpInst::ICMP_SGE;
3468 }
3469}
3470
Craig Topper1c3f2832015-12-15 06:11:33 +00003471bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003472 switch (predicate) {
3473 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003474 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003475 case ICmpInst::ICMP_UGE: return true;
3476 }
3477}
3478
Craig Topper1c3f2832015-12-15 06:11:33 +00003479bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003480 switch (predicate) {
3481 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003482 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
Reid Spencer266e42b2006-12-23 06:05:41 +00003483 case ICmpInst::ICMP_SGE: return true;
3484 }
3485}
3486
Craig Topper1c3f2832015-12-15 06:11:33 +00003487bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003488 switch (predicate) {
3489 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003490 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3491 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003492 case FCmpInst::FCMP_ORD: return true;
3493 }
3494}
Fangrui Songf78650a2018-07-30 19:41:25 +00003495
Craig Topper1c3f2832015-12-15 06:11:33 +00003496bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003497 switch (predicate) {
3498 default: return false;
Fangrui Songf78650a2018-07-30 19:41:25 +00003499 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3500 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
Reid Spencer266e42b2006-12-23 06:05:41 +00003501 case FCmpInst::FCMP_UNO: return true;
3502 }
3503}
3504
Craig Topper1c3f2832015-12-15 06:11:33 +00003505bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003506 switch(predicate) {
3507 default: return false;
3508 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3509 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3510 }
3511}
3512
Craig Topper1c3f2832015-12-15 06:11:33 +00003513bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003514 switch(predicate) {
3515 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3516 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3517 default: return false;
3518 }
3519}
3520
Chad Rosier99bc4802016-04-21 16:18:02 +00003521bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003522 // If the predicates match, then we know the first condition implies the
3523 // second is true.
3524 if (Pred1 == Pred2)
3525 return true;
3526
3527 switch (Pred1) {
3528 default:
3529 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003530 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003531 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003532 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3533 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003534 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3535 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3536 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3537 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3538 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3539 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3540 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3541 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003542 }
3543 return false;
3544}
3545
Chad Rosier99bc4802016-04-21 16:18:02 +00003546bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003547 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003548}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003550//===----------------------------------------------------------------------===//
3551// SwitchInst Implementation
3552//===----------------------------------------------------------------------===//
3553
Chris Lattnerbaf00152010-11-17 05:41:46 +00003554void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3555 assert(Value && Default && NumReserved);
3556 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003557 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003558 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003559
Pete Coopereb31b682015-05-21 22:48:54 +00003560 Op<0>() = Value;
3561 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003562}
3563
Chris Lattner2195fc42007-02-24 00:55:48 +00003564/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3565/// switch on and a default destination. The number of additional cases can
3566/// be specified here to make memory allocation more efficient. This
3567/// constructor can also autoinsert before another instruction.
3568SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3569 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003570 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003571 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003572 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003573}
3574
3575/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3576/// switch on and a default destination. The number of additional cases can
3577/// be specified here to make memory allocation more efficient. This
3578/// constructor also autoinserts at the end of the specified BasicBlock.
3579SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3580 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003581 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003582 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003583 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003584}
3585
Misha Brukmanb1c93172005-04-21 23:48:37 +00003586SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003587 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003588 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003589 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003590 Use *OL = getOperandList();
3591 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003592 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003593 OL[i] = InOL[i];
3594 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003595 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003596 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003597}
3598
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003599
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003600/// addCase - Add an entry to the switch instruction...
3601///
Chris Lattner47ac1872005-02-24 05:32:09 +00003602void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003603 unsigned NewCaseIdx = getNumCases();
3604 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003605 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003606 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003607 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003608 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003609 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003610 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003611 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003612 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003613}
3614
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003615/// removeCase - This method removes the specified case and its successor
3616/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003617SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3618 unsigned idx = I->getCaseIndex();
3619
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003620 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003621
3622 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003623 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003624
Jay Foad14277722011-02-01 09:22:34 +00003625 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003626 if (2 + (idx + 1) * 2 != NumOps) {
3627 OL[2 + idx * 2] = OL[NumOps - 2];
3628 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003629 }
3630
3631 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003632 OL[NumOps-2].set(nullptr);
3633 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003634 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003635
3636 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003637}
3638
Jay Foade98f29d2011-04-01 08:00:58 +00003639/// growOperands - grow operands - This grows the operand list in response
3640/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003641///
Jay Foade98f29d2011-04-01 08:00:58 +00003642void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003643 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003644 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003645
3646 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003647 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003648}
3649
Chris Lattner3ed871f2009-10-27 19:13:16 +00003650//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003651// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003652//===----------------------------------------------------------------------===//
3653
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003654void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003655 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003656 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003657 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003658 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003659 allocHungoffUses(ReservedSpace);
3660
Pete Coopereb31b682015-05-21 22:48:54 +00003661 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003662}
3663
3664
Jay Foade98f29d2011-04-01 08:00:58 +00003665/// growOperands - grow operands - This grows the operand list in response
3666/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003667///
Jay Foade98f29d2011-04-01 08:00:58 +00003668void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003669 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003670 unsigned NumOps = e*2;
Fangrui Songf78650a2018-07-30 19:41:25 +00003671
Chris Lattner3ed871f2009-10-27 19:13:16 +00003672 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003673 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003674}
3675
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003676IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3677 Instruction *InsertBefore)
3678: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003679 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003680 init(Address, NumCases);
3681}
3682
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003683IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3684 BasicBlock *InsertAtEnd)
3685: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003686 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003687 init(Address, NumCases);
3688}
3689
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003690IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003691 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3692 nullptr, IBI.getNumOperands()) {
3693 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003694 Use *OL = getOperandList();
3695 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003696 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3697 OL[i] = InOL[i];
3698 SubclassOptionalData = IBI.SubclassOptionalData;
3699}
3700
Chris Lattner3ed871f2009-10-27 19:13:16 +00003701/// addDestination - Add a destination.
3702///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003703void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003704 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003705 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003706 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003707 // Initialize some new operands.
3708 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003709 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003710 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711}
3712
3713/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003714/// indirectbr instruction.
3715void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003716 assert(idx < getNumOperands()-1 && "Successor index out of range!");
Fangrui Songf78650a2018-07-30 19:41:25 +00003717
Chris Lattner3ed871f2009-10-27 19:13:16 +00003718 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003719 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003720
3721 // Replace this value with the last one.
3722 OL[idx+1] = OL[NumOps-1];
Fangrui Songf78650a2018-07-30 19:41:25 +00003723
Chris Lattner3ed871f2009-10-27 19:13:16 +00003724 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003725 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003726 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003727}
3728
Chris Lattner3ed871f2009-10-27 19:13:16 +00003729//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003730// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003731//===----------------------------------------------------------------------===//
3732
Chris Lattnerf22be932004-10-15 23:52:53 +00003733// Define these methods here so vtables don't get emitted into every translation
3734// unit that uses these classes.
3735
Pete Cooper75403d72015-06-24 20:22:23 +00003736GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003737 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003738}
3739
Pete Cooper75403d72015-06-24 20:22:23 +00003740BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003741 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003742}
3743
Pete Cooper75403d72015-06-24 20:22:23 +00003744FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003745 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003746}
3747
Pete Cooper75403d72015-06-24 20:22:23 +00003748ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003749 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003750}
3751
Pete Cooper75403d72015-06-24 20:22:23 +00003752ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003753 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003754}
3755
Pete Cooper75403d72015-06-24 20:22:23 +00003756InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003757 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003758}
3759
Pete Cooper75403d72015-06-24 20:22:23 +00003760AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003761 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003762 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003763 (Value *)getOperand(0), getAlignment());
3764 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003765 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003766 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003767}
3768
Pete Cooper75403d72015-06-24 20:22:23 +00003769LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003770 return new LoadInst(getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003771 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003772}
3773
Pete Cooper75403d72015-06-24 20:22:23 +00003774StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003775 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003776 getAlignment(), getOrdering(), getSyncScopeID());
Fangrui Songf78650a2018-07-30 19:41:25 +00003777
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003778}
3779
Pete Cooper75403d72015-06-24 20:22:23 +00003780AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003781 AtomicCmpXchgInst *Result =
3782 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003783 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003784 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003785 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003786 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003787 return Result;
3788}
3789
Pete Cooper75403d72015-06-24 20:22:23 +00003790AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003791 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003792 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3793 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003794 Result->setVolatile(isVolatile());
3795 return Result;
3796}
3797
Pete Cooper75403d72015-06-24 20:22:23 +00003798FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003799 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003800}
3801
Pete Cooper75403d72015-06-24 20:22:23 +00003802TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003803 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003804}
3805
Pete Cooper75403d72015-06-24 20:22:23 +00003806ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003807 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003808}
3809
Pete Cooper75403d72015-06-24 20:22:23 +00003810SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003811 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003812}
3813
Pete Cooper75403d72015-06-24 20:22:23 +00003814FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003815 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003816}
3817
Pete Cooper75403d72015-06-24 20:22:23 +00003818FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003819 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003820}
3821
Pete Cooper75403d72015-06-24 20:22:23 +00003822UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003823 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003824}
3825
Pete Cooper75403d72015-06-24 20:22:23 +00003826SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003827 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003828}
3829
Pete Cooper75403d72015-06-24 20:22:23 +00003830FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003831 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003832}
3833
Pete Cooper75403d72015-06-24 20:22:23 +00003834FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003835 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003836}
3837
Pete Cooper75403d72015-06-24 20:22:23 +00003838PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003839 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003840}
3841
Pete Cooper75403d72015-06-24 20:22:23 +00003842IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003843 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003844}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003845
Pete Cooper75403d72015-06-24 20:22:23 +00003846BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003847 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003848}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003849
Pete Cooper75403d72015-06-24 20:22:23 +00003850AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003851 return new AddrSpaceCastInst(getOperand(0), getType());
3852}
3853
Pete Cooper75403d72015-06-24 20:22:23 +00003854CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003855 if (hasOperandBundles()) {
3856 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3857 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3858 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003859 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003860}
3861
Pete Cooper75403d72015-06-24 20:22:23 +00003862SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003863 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003864}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003865
Pete Cooper75403d72015-06-24 20:22:23 +00003866VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003867 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003868}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003869
Pete Cooper75403d72015-06-24 20:22:23 +00003870ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003871 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003872}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003873
Pete Cooper75403d72015-06-24 20:22:23 +00003874InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003875 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003876}
3877
Pete Cooper75403d72015-06-24 20:22:23 +00003878ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003879 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003880}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003881
Pete Cooper75403d72015-06-24 20:22:23 +00003882PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003883
Pete Cooper75403d72015-06-24 20:22:23 +00003884LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003885 return new LandingPadInst(*this);
3886}
3887
Pete Cooper75403d72015-06-24 20:22:23 +00003888ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003889 return new(getNumOperands()) ReturnInst(*this);
3890}
3891
Pete Cooper75403d72015-06-24 20:22:23 +00003892BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003893 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003894}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003895
Pete Cooper75403d72015-06-24 20:22:23 +00003896SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003897
Pete Cooper75403d72015-06-24 20:22:23 +00003898IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003899 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003900}
3901
Pete Cooper75403d72015-06-24 20:22:23 +00003902InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003903 if (hasOperandBundles()) {
3904 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3905 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3906 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003907 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003908}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909
Pete Cooper75403d72015-06-24 20:22:23 +00003910ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003911
David Majnemer654e1302015-07-31 17:58:14 +00003912CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3913 return new (getNumOperands()) CleanupReturnInst(*this);
3914}
3915
David Majnemer654e1302015-07-31 17:58:14 +00003916CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003917 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003918}
3919
David Majnemer8a1c45d2015-12-12 05:38:55 +00003920CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3921 return new CatchSwitchInst(*this);
3922}
3923
3924FuncletPadInst *FuncletPadInst::cloneImpl() const {
3925 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003926}
3927
Pete Cooper75403d72015-06-24 20:22:23 +00003928UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003929 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003930 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003931}